【发布时间】:2021-05-19 02:31:03
【问题描述】:
常规的 JS 函数、辅助/服务函数和 React 自定义 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 等。那么这个函数仍然是一个自定义钩子吗?它只是一个实用程序/辅助函数吗?
当我们谈论 Custom Hooks & Utility/Helper/Service/Normal Functions 等时,到底有什么区别...
【问题讨论】:
标签: reactjs