How To Get Previous State In ReactJS Functional Component? Last Updated : 14 Oct, 2024 Comments Improve Suggest changes Like Article Like Report To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task.PrerequisitesReact JSReact JS functional componentsReact useState HookWhy Accessing the Previous State is Important in React?In many real-world applications, accessing the previous state can be necessary to ensure proper behaviour or user experience. For example, you might want to:Implement animations based on state changes.Keep track of a user’s actions to enable undo or redo functionalities.Compare the current state with the previous state to determine if a specific condition is met.React’s functional components do not have a direct way to access the previous state like class-based components do, but we can achieve this using hooks.Steps To Get Previous State In ReactJS Create A React Application using the following command.npm create-react-app myreactappcd myreactappProject StructureFolder StructureExample: This example demonstrate the use of setstate method form usestate to get the previoues state an display in another state variable. JavaScript // Filename - App.js import React, { useState } from "react"; const App = () => { const [number, setNumber] = useState(0); const [previousValue, setPreviousValue] = useState(null); return ( <div style={{ textAlign: "center", alignItems: "center", margin: "auto", }} > <h1 style={{ color: "green" }}> GeeksforGeeks </h1> <h3> React Example to excess previous state in Funtional components </h3> <h4>number: {number}</h4> <h4>previous number: {previousValue}</h4> <button onClick={() => setNumber((previous) => { setPreviousValue(previous); return previous + 1; }) } > increment </button> </div> ); }; export default App; To start the application run the following command.npm startOutput: This output will be visible on the http://localhost:3000/ on the browser window.Get Previous State In ReactJS Functional Component Comment More infoAdvertise with us Next Article How To Get Previous State In ReactJS Functional Component? M mahmoudElsheikh1 Follow Improve Article Tags : Web Technologies ReactJS Similar Reads State in React Functional Component State in React functional components refers to a way of managing data that influences the rendering and behavior of components. Unlike class components, where state is managed using a this.state object, functional components use the React Hook useState to manage state efficiently.State is Local to t 3 min read How to access props.children in a stateless functional component in ReactJS ? The {props. children} allows you to create a generic template, which can be modified by a parent when invoked. In this way, a parent component can easily pass whatever is necessary to its child, even generated layout features. Anytime you call a component, it automatically displays whatever it has b 5 min read How to create a Functional Component in React? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b 2 min read How to create Functional Components in React ? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b 2 min read How to use useMemo Hook in a Functional Component in React ? In React development, optimizing performance is crucial for delivering responsive and fast user experiences. One way to achieve this is by using the `useMemo` hook. In this article, we'll explore what `useMemo` is, why it's beneficial, and how to use it effectively in functional components. Table of 3 min read Like