Published on

Update two states after fetch

Author

When we need to fetch something and update 2 React states, how should we proceed?

  • React 16/17 has a unstable_batchedUpdates() function to update states in batch
import { unstable_batchedUpdates } from 'react-dom'

unstable_batchedUpdates(() => {
  setName('Moustafa')
  setAge(25)
})
  • React 18 makes that automatically
  • Other alternative would be making a unique state with all props that would change