Answer the question
In order to leave comments, you need to log in
What is a side effect?
I'm learning React + Redux. Explain that side effects should be in the middleware. Can't figure out what side effects are? Please explain in simple terms. And why should they be used in middleware?
For example, an example of adding a new comment to an article is given. The author says that the comment id should be generated in the middleware. But we could do it in the reducer. Why is that?
Answer the question
In order to leave comments, you need to log in
Sideeffect is something that can affect the "purity" of your function. The reducer is a function. A pure function means that if you give it the same parameters as input, the result will always be the same.
Example: you have a username in localStorage. And you write something like this in your code:
case SET_DISPLAY_NAME: {
return {
...state,
name: window.localStorage.getItem('name') ? window.localStorage.getItem('name') : action.payload,
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question