State Management in React

Introduction


In React, state management is the crucial topic and without this topic , react cannot re-render or revaluate the components. React is all about the state management , we can do the state management in react with React Hooks and Redux.

Why State Management?

Once , you initialize the react app and run on the browser it will render all the html , components in the react and the sub components only in single page. React doesn't reload when variable initialize again or when some datatype changes with some result also not in  conditional statements  neither in loop statements. React renders all the code only for once, and for this need to not reload the page again when some variable changes , it only changes that block of statement or component whenever the state changes.

State management can be done with useState , useContext , useReducer and with Redux library for advance state management. But in this article we'll only discuss about the useState hook deeply and understand it better.

What is UseState Hook?

React allows the re-rendering and re-evaluating the component in the functional component with the useState hook .
Previously , when working with classes we cannot use hooks for the state management and functional components are called as stateless components. State management can only be done in class components but with the React 16.8 version or higher , we can use hooks and make the functional components to be stateful components

UseState hook are used for the state management and it re-renders and re-evaluated the component whenever the state changes. Let's see how to do the state management in react with the useState hook.

How UseState Hook?

First,  we need to create the react app and  namely import the useState hook from the react library. Because React library is responsible for component updates and state management in react.
























UseState hook in react has two values , first is current value of state and second is setter function , which will be used to update the state in the component.
Use state hook is the function from where we can set the default value of our state.















In this example , the count and set count are the values of the useState whose default value is 0 where the count will store the current value and setCount will update the state , so the whole component re-runs again and the state changes.
Whenever the button is clicked the setter function will be called and the count value shows in the web browser in the h1 tag.

This is the basic example of useState hook where we have update the state and our count value changes . In the next blog we will see the useState hook with object and later in useState hook in array.

I will share the code sandbox link , so you can practice and play with the code!
https://codesandbox.io/s/compassionate-dust-njnj63?file=/src/App.js

Thank you for reading the blog post!

Comments

Popular posts from this blog

Learn React Router v6 ( Part - 2)

Why use Typescript ?

What is Single Page Application Routing ?