![]() |
While developing a React Application, it is very important to manage the state efficiently for building fully functional applications. As React is growing there are multiple approaches introduced that can help to manage state efficiently depending upon the type of application we are building. We will discuss the following state management techniques: Table of Content Before we dive deep into state management, let us first understand what is state in React. What is state in React ?React JS State is a way to store and manage the information or data while creating a React Application. The state is a JavaScript object that contains the real-time data or information on the webpage. The state in React is an instance of the React Component that can be defined as an object of a set of observable properties that control the behavior of the component. In other words, the State of a component is an object that holds some information that may change over the lifetime of the component. Approach 1: Manage states using HooksHooks were introduced in React 16.8, which makes a great revolution in functional components by helps them to manage state and lifecycle features.
Syntax: const [state, setState] = useState(<default value>); useReducer hook is the better alternative to the useState hook and is generally more preferred over the useState hook when you have complex state-building logic or when the next state value depends upon its previous value or when the components are needed to be optimized. Syntax: const [state, dispatch] = useReducer(reducer, initialArgs, init); Example: In this example we will see how we can manage state with the help of useState Hook. Javascript
Output: ![]() useState example output Approach 2: Manage states using Context APIContext provides a way to pass data or state through the component tree without having to pass props down manually through each nested component. It is designed to share data that can be considered as global data for a tree of React components, such as the current authenticated user or theme(e.g. color, paddings, margins, font-sizes). Consumer Components pass down the data but it is very difficult to write the long functional code to use this Context API. So useContext hook helps to make the code more readable, less expensive and removes the need to introduce Consumer Component. The useContext hook is the new addition in React 16.8. Syntax: const authContext = useContext(initialValue); Example: In this example we will see the basic implementation of context API. Javascript
Javascript
Javascript
Output: ![]() Context API example output Approach 3: Manage states using ReduxRedux is a state managing library used in JavaScript apps. It simply manages the state of your application or in other words, it is used to manage the data of the application. It is used with a library like React. It makes easier to manage state and data. As the complexity of our application increases. Building Blocks of redux: Example: In this example we will see the basic implementation of Redux. Install dependency to use Redux in your application npm install redux react-redux Javascript
Javascript
Javascript
Javascript
Javascript
Output: ![]() Redux example output ConclusionBy this article you have learned how to manage states using the useState and the useReducer hooks, the React Context API and the Redux Toolkit. Its totally depend upon the type of project to choose between these options:
|
Reffered: https://www.geeksforgeeks.org
ReactJS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |