Cauc-Query Crud作用的定制钩子
我正在努力封装我的react-Query
在自定义钩中订单的CRUD操作。首先,我已经封装了getallorders
Opertion并运行良好,但是现在我试图使用getDetails
来做同样的事情,并且我有此错误消息:
React Hook“ useQuery”在函数“顺序”中称为React函数组件,也不是自定义React Hook函数。 React组件名称必须以大写字母开头。 React Hook名称必须以“使用” React钩/钩规则
开始
因为它不是在组件或钩子上执行的,而是在钩子内部的函数内部执行的查询。
我不想将参数传递到钩子上,因为并非总是需要一个订单详细信息。
到目前为止,这是我的钩子:
const useOrders = () => {
const session = useContext(Context);
const token = session.storedValue.token;
const orders = useQuery(["orders", token], () => {
const result = getOrders(token);
return result;
});
const order = (orderId) => {
const query = useQuery(["order", token, orderId], () => {
const result = getOrderDetails(token, orderId);
return result;
});
return query;
};
return { orders, order };
};
我想将OrderID
传递给order
函数并获取查询。
I'm working on encapsulating my react-query
CRUD operations for Orders in a custom hook. First I've encapsulated the getAllOrders
opertion and worked well but now I'm trying to do the same with the getDetails
and I'm having this error message:
React Hook "useQuery" is called in function "order" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks
It's complaining because the query it's not being executed on a component or hook but inside a function inside a hook.
I don't want to pass a parameter to the hook because not always I'll need one order details.
This is my hook so far:
const useOrders = () => {
const session = useContext(Context);
const token = session.storedValue.token;
const orders = useQuery(["orders", token], () => {
const result = getOrders(token);
return result;
});
const order = (orderId) => {
const query = useQuery(["order", token, orderId], () => {
const result = getOrderDetails(token, orderId);
return result;
});
return query;
};
return { orders, order };
};
I'd like to pass the orderId
to the order
function and get query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当调用钩子时,它看起来像是2个单独的钩子
,有时您需要所有订单,有时您会搜索一个订单。使这两个不同的钩子根据需要使用
It looks like 2 separate hooks to me
Be more definite when calling hooks, sometimes you want all orders and sometimes you search for one. make these 2 different hooks for use as required