Components with internal state
Let's do a little bit more with the button. Let's make it count clicks.
Naive implementation
Boom, done. Except it doesn't work, because React has no idea that it should rerender the Clicker
component or keep track of the clicks
variable.
Using the useState
hook
useState
hookCurrent Industry Best Practice ™ for keeping track of the state. Previously, we would use class components, but hooks have some advantages over them.
Rules of Hooks
There are just two rules you have to remember to use Hooks properly:
Only Call Hooks at the Top Level
Only Call Hooks from React Functions
Some people say there are just two rules but reality is that they exist to address problems with magic used to implement Hooks.
They make using Hooks harder.
Resources
Last updated