Skip to content

dispatch while render? #1943

Answered by markerikson
arcanis asked this question in Q&A
Discussion options

You must be logged in to vote

No, never.

The use case of calling setState() while rendering only works because React special-cases that exact behavior. If it detects that you tried to do a setState() while rendering, it queues up that state update and immediately, synchronously re-runs the rendering for that component before continuing. That means that conceptually this can sorta be seen as "not actually a side effect", because the behavior is never visible to the outside world (similar to creating a new object/array in a function, mutating it, and then returning it - no one else ever saw it being mutated, so it's okay in that case).

With Redux, on the hand, dispatching an action while rendering is most definitely a s…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by arcanis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants