DEV Community

dannohh
dannohh

Posted on

React Hooks and state

With React Hooks, we can give our functional components some state to keep track of and render() with said component or even children components. To do this we can use the useState() hook to set our initial state and the setState() method for updating. When we want to do something AFTER our component renders for the first time, (Mounts) we can use the useEffect() method. So, we set the initial state of our component with the useState() method and update the state with the setState() method.

Being able to manipulate and set the state of our functional components allows more freedom and flexibility while building or Apps with React. For example, when we want a small component to be able to keep track of, and update its own state without rendering it through a parent component, we could use a hook instead of the traditional Class component with a constructor, saving time, space and making our code easier to read for humans! Sweet!

Top comments (0)