Skip to content
Myr

event-handler-in-pre-react-18.md

This file

Path

docs/learn/guides/event-handler-in-pre-react-18.md

24 lines · 827 bytes · Markdown

Last changed by

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

History of this file

Take it

Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9

lines 1–24why, one step back
1---
2title: Calling actions outside a React event handler in pre React 183nav: 114---56Because React handles `setState` synchronously if it's called outside an event handler, updating the state outside an event handler will force react to update the components synchronously. Therefore, there is a risk of encountering the zombie-child effect.7In order to fix this, the action needs to be wrapped in `unstable_batchedUpdates` like so:89```jsx10import { unstable_batchedUpdates } from 'react-dom' // or 'react-native'1112const useFishStore = create((set) => ({13 fishes: 0,14 increaseFishes: () => set((prev) => ({ fishes: prev.fishes + 1 })),15}))1617const nonReactCallback = () => {18 unstable_batchedUpdates(() => {19 useFishStore.getState().increaseFishes()20 })21}22```2324More details: https://github.com/pmndrs/zustand/issues/302