I am new to react and redux and I am facing a very strange issue. I have an action as follows:
export function getStorySnippetsAction(snippetsLink, channel) {
return dispatch => {
let storiesSnoppet = [];
for (var i = 0; i < snippetsLink.length; i++) {
$.ajax({
url: snippetsLink[i],
dataType: 'json',
cache: false,
success: function (data) {
storiesSnoppet.push(data);
console.log("xxxxxxxxxxxxxxxxxxxxx");
console.log(storiesSnoppet);
}.bind(this)
});
}
dispatch({
type: "SET_STORY_SNIPPET",
payload: {"channel": channel, "storiesSnippet": storiesSnoppet}
});
};
}
As you can see I have some ajax calls in a loop and then I use dispach to set state. Now my problem is ajax calls are asynchronous so when ajax sends request the dispach happens and does not wait for all ajax call to finish. Also the following is what I get when I print out the payload.action in console:
As you can see when the object is collapsed it has a size of 0 and when I expand it I can see some objects in an arraylist. My only guess for such a strange behavior is that the asynch call complete after the rendering and since componentdidmount( which I call getStorySnippetsAction in it) happens just once the rerender does not happen again and I do not see any result. Someone told be to put my call to getStorySnippetsAction inside componentdidupdate but when I do that then it seems that an infinite loop happens and the page never loads ( I can see that in the console the same thing is written again and gain which means that component did update invokes infinitely). Now I am totally stuck. If I call getStorySnippetsAction in componentdDidUpdate I will be in an infinite loop and if I call it in componentDidMount ajax takes longer that rendering and rendering happens with empty array instead of loading the ajax result.
Can anyone help? (Even an idea may help me to fix this issue. Thanks a million)