T
TanStack4y ago
rival-black

mutate in loop cause infinite re renders

usecase: I have orders coming from other hooks wraped around useQuery I am getting orders from those hook What i need to do loop through all orders, get few properties and fire mutation inside loop, get data from previous mutation and fire other mutation . Problem mutation causing infinite re renders (used async mutation), code
for (let i in allOrders) {

let customerDetails = {};
let productDetails = {};
let orderDetails = {};

customerDetails.name = allOrders[i].shipping_address.name;

customerDetails.address =
allOrders[i].shipping_address.address1 +
" " +
allOrders[i].shipping_address.address2;

customerDetails.phone = allOrders[i].shipping_address.phone;

customerDetails.pincode = allOrders[i].shipping_address.zip;
customerDetails.province_code = allOrders[i].shipping_address.province_code;
customerDetails.city = allOrders[i].shipping_address.city;
customerDetails.state = allOrders[i].shipping_address.province;

// want to mutate(customerDetails) here and get data from mutation


// mutate orders (customer id from previous mutation)
}
for (let i in allOrders) {

let customerDetails = {};
let productDetails = {};
let orderDetails = {};

customerDetails.name = allOrders[i].shipping_address.name;

customerDetails.address =
allOrders[i].shipping_address.address1 +
" " +
allOrders[i].shipping_address.address2;

customerDetails.phone = allOrders[i].shipping_address.phone;

customerDetails.pincode = allOrders[i].shipping_address.zip;
customerDetails.province_code = allOrders[i].shipping_address.province_code;
customerDetails.city = allOrders[i].shipping_address.city;
customerDetails.state = allOrders[i].shipping_address.province;

// want to mutate(customerDetails) here and get data from mutation


// mutate orders (customer id from previous mutation)
}
1 Reply
absent-sapphire
absent-sapphire4y ago
show a codesandbox reproduction please

Did you find this page helpful?