Trigger same API call with different payloads in parallel using CreateResource
I am facing issues with making same API call multiple times with different payloads (using ForEach). I am setting a payload within the store at each iteration and that store proxy is used as a dependency of fetcher function inside createResource
export const TriggerAPI = () => {
const [dummy] = createResource(getPayload, fetcher);
return dummy;
};
getPayload is return values from store. I verified that it is updating each time in loop but API is triggering only once. Can someone help what should be the ideal way to handle it7 Replies
Can you show a bit more of the surrounding code and tell us more about the environment you're in?
Hey Alex , Thanks for replying . I figured out the issue but couldn't understand it fully . The problem was the getPayload function was returning a store object but if we just return payload directly from store it wasn't working . Changing it to returning a new object worked.
function getPayload() {
if (condition = true) {
return payloadFromStore;
}
return;
}
to this function getPayload() {
if (condition = true) {
return {...payloadFromStore};
}
return;
}
My concern is that the store object was anyways updated but then why creating a new object (using spread) worked but former one didn'tThe store object itself is not reactive, only its properties are.
So by returning the store object itself, you do not subscribe any properties.
The shallow cloning subscribes all top-layer properties, so changes to them trigger reactive effects.
I see. Do you think what I did above is correct ? BTW in each iteration I am updating the values of a key inside the payloadFromStore object.
I don't know how getPayload() is used, so I cannot really say.
Sure Alex..
Thanks for your help .. It worked well
Glad to hear that. 👍