The Several States of the`useState` Hook in React

Travis Waith-Mair
Non-Traditional Dev
5 min readSep 17, 2020

--

Photo by Caspar Camille Rubin on Unsplash

Hooks have changed the way we write React components. It encourages a much more functional and composable style of coding that was difficult to achieve with class components. One of the biggest game-changers is the useState hook.

Before hooks, React state was managed by a single state property on the class component. This property had to be an object and had to be updated with a class method called this.setState which despite its name, didn’t set the state, it patched the state with whichever object we provided it. Though this worked well, it encouraged us to throw all the state in this one object, even though the value were not related or updated together. It wouldn’t be uncommon to have a state object look like this:

{
toolTipIsVisible:false,
userList:[...],
formData:{...},
matchesMedia:false,
inViewPort:true,
....
}

All this state was data that need to be saved and maintained, but they didn’t need to be maintained together nor live in the same object, other than this was the limitation of the class component. This also made refactoring difficult, because it wasn’t always obvious which parts of the state needed to stay together and could be lifted into a parent component or dropped into a child component.

--

--