Re:Two custom React hooks
by drdec on Friday December 17, 2021 @08:02PM
(#29600343)
The docs for setState say that if passed an object it will merge the current state and the object. So just do:
https://reactjs.org/docs/react-component.html#setstate
[ Parent
| on HN ]
The docs you linked to are for class components (`this.setState`), not function components using hooks (`useState`).
From React's "Using the State Hook" docs:
> However, unlike this.setState in a class, updating a state variable always replaces it instead of merging it.
https://reactjs.org/docs/hooks-state.html#tip-using-multiple...
The `extendState` method gives you the same convenience of `this.setState` but with hooks in function components instead of class components.