0

I'm trying to dispatch a redux action with an array of id which i'm sending to a get request which takes the id and returns a new json for each time item of id im sending, the array im sending has 10 ID and i want to make 10 requests for each id ( basically loop for the ID and call the getRoles() 10 times with different ID's)

This is the code im sending

useEffect(() => {
    !!idList && !!users && idList.map(id => dispatch(usersRolesActions.getRoles(id)));
  }, [idList, dispatch]);

idList is the array with 10 id's, and i want to call the getRoles thunk 10 times and pass the id but the dispatch is calling the getRoles(id) thunk 10 times but returning results with only the first idList item 10 times

Mentor_Aliu
  • 11
  • 1
  • 4
  • I don't think your question is very clear. What did you expect to happen and what is actually happening? The thunk is called 10 times, each time with the correct id? But then when the requests return, they all have results for the same id, is that it? – Bernardo Ferreira Bastos Braga Mar 02 '22 at 14:48
  • Hi, yes thats correct, i have an array with 10 id's and im trying to loop those id's in a fetch call with each id in the array and get a different result for each id, instead of getting different results for each id, i'm getting the same response 10 time with only the first id applied to the fetch, it ignores 9 other ID's i have in the array – Mentor_Aliu Mar 03 '22 at 08:13
  • If thats the case, it could very well be a problem with the reducer. Have you tried logging the id inside the getroles function? Otherwise maybe you're caching or memoizing the callback that receives the results? Besides not including the users variable in the dependency array, I see nothing wrong with this code snippet – Bernardo Ferreira Bastos Braga Mar 03 '22 at 09:37

0 Answers0