Can I set state inside a useEffect hook

后端 未结 5 584
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 21:20

Lets say I have some state that is dependent on some other state (eg when A changes I want B to change).

Is it appropriate to create a hook that observes A and sets

5条回答
  •  余生分开走
    2020-11-30 21:21

    Effects are always executed after the render phase is completed even if you setState inside the one effect, another effect will read the updated state and take action on it only after the render phase.

    Having said that its probably better to take both actions in the same effect unless there is a possibility that b can change due to reasons other than changing a in which case too you would want to execute the same logic

提交回复
热议问题