Rules for managing side effects in react

Kamal Neupane

Kamal NeupaneApril 04, 2025

1 min read119 words

thumbnail.jpg

Rules for managing side effects in react

0
Rule #0
When a component renders, it should do so without running into any side effects
1
Rule #1
If a side effect is triggered by an event, put that side effect in an event handler
2
Rule #2
If a side effect is synchronizing your component with some outside system, put that side effect inside useEffect Rule
3
Rule #3
If a side effect is synchronizing your component with some outside system and that side effect needs to run before the browser paints the screen, put that side effect inside useLayoutEffect
3
Rule #4
If a side effect is subscribing to an external store, use the useSyncExternalStore hook