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 it
7 Replies
Alex Lohr
Alex Lohr2y ago
Can you show a bit more of the surrounding code and tell us more about the environment you're in?
Abrinzer
Abrinzer2y ago
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't
Alex Lohr
Alex Lohr2y ago
The 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.
Abrinzer
Abrinzer2y ago
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.
Alex Lohr
Alex Lohr2y ago
I don't know how getPayload() is used, so I cannot really say.
Abrinzer
Abrinzer2y ago
Sure Alex.. Thanks for your help .. It worked well
Alex Lohr
Alex Lohr2y ago
Glad to hear that. 👍
Want results from more Discord servers?
Add your server