常规 JS 函数、帮助器/服务函数和 React Custom Hook 之间有什么区别?
我发现的类似答案没有给出任何好的例子,所以我创建了这个“服务”函数,我将使用它来列出其他函数组件中的一些用户:
import axios from "axios";
const useService = () => {
const fields = [
{ key: "name", _style: { width: "40%" } },
{ key: "email", _style: { width: "40%" } },
];
const getUsers = async () => {
return await axios.get("http://127.0.0.1:8000/api/users");
};
const getBadge = (status) => {
switch (status) {
case "Active":
return "success";
case "Inactive":
return "secondary";
case "Pending":
return "warning";
case "Banned":
return "danger";
default:
return "primary";
}
};
return { fields, getUsers, getBadge };
};
export default useService;
我调用了它,useService
但我实际上并没有在这里使用任何 React 钩子,例如 useEffect
,useState
等等。那么这个函数仍然是一个自定义钩子吗?它只是一个实用程序/辅助功能吗?
当我们谈论自定义钩子和实用程序/助手/服务/正常功能等时,究竟有什么区别......