[英]How do I call react function in js map?
I have the below code, the problem is in the part where I am calling the RemoveProduct function in the map function:我有以下代码,问题出在我在 map 函数中调用 RemoveProduct 函数的部分:
function AddOrder() {
const d = new Date();
let text = d.toString();
const [addOrder] = useMutation(queries.ADD_ORDER);
const [editUser] = useMutation(queries.EDIT_USER_CART);
const [editProduct] = useMutation(queries.EDIT_PRODUCT);
function RemoveProduct (x) {
let getProd = useQuery(queries.GET_PRODUCTS_BY_ID, {
fetchPolicy: "network-only",
variables: {
_id : x._id
},
});
console.log(getProd.quantity)
console.log(x.quantity)
let a = getProd.quantity - x.quantity
console.log(a)
editProduct({
variables: {
_id : x._id,
quantity: a
},
});
return null;
}
const { currentUser } = useContext(AuthContext);
if (error) {
return <h1> error</h1>;
} else if (loading) {
return <h1> loading</h1>;
} else if (data && getUserOrders.data && currentUser && data.getUser.cart.length > 0) {
let newCart = [];
let total = 0;
for (let i = 0; i < data.getUser.cart.length; i++) {
total += data.getUser.cart[i].price * data.getUser.cart[i].quantity;
newCart.push({
orderedQuantity: data.getUser.cart[i].quantity,
_id: data.getUser.cart[i]._id,
name: data.getUser.cart[i].name,
image: data.getUser.cart[i].image,
price: data.getUser.cart[i].price,
});
}
newCart.map((x) => RemoveProduct(x))
editUser({
variables: {
id: currentUser.uid,
cart: [],
},
});
}
}
export default AddOrder;
I get the following error when I run this code:运行此代码时出现以下错误:
Uncaught Error: Rendered more hooks than during the previous render.
How can I fix this?我怎样才能解决这个问题? I tried to create a separate component for RemoveProducts and call in this function but that did not work either.我尝试为 RemoveProducts 创建一个单独的组件并调用此函数,但这也不起作用。
I read this from the docs here我从这里的文档中读到了这个
So try not to nest the function RemoveProduct or remove the hook called inside it所以尽量不要嵌套函数 RemoveProduct 或删除其中调用的钩子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.