ReactJS getSnapshotBeforeUpdate() Method
Last Updated :
03 Oct, 2024
The getSnapshotBeforeUpdate() method is invoked just before the DOM is being rendered. It is used to store the previous values of the state after the DOM is updated.
Syntax:
getSnapshotBeforeUpdate(prevProps, prevState)
Parameters:
It accepts two parameters, they are prevProps and prevState which are just the props or state before the component in question is re-rendered.
Return:
The return value can be any object, number, or string. Also, it can be null when no information need to be captured.
Any value returned by getSnapshotBeforeUpdate() method will be used as a parameter for componentDidUpdate() method. This function is always used along with the componentDidUpdate() method but vice-versa isn't true.
Steps to Create React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:

Example: Program to demonstrate the use of getSnapshotBeforeUpdate() method. Here, we are going to use the previous and current values of the state to display some text.
JavaScript
// Filename: App.js
import React from "react";
class App extends React.Component {
// Initializing the state
state = {
name: "GFG"
};
componentDidMount() {
// Changing the state after 1 sec
setTimeout(() => {
this.setState({ name: "GeeksForGeeks" });
}, 1000);
}
getSnapshotBeforeUpdate(prevProps, prevState) {
// Displaying the previous value of the state
document.getElementById("prev").innerHTML =
"Previous Name: " + prevState.name;
}
componentDidUpdate() {
// Displaying the current value of the state
document.getElementById("new").innerHTML =
"Current Name: " + this.state.name;
}
render() {
return (
<div>
<div id="prev"></div>
<div id="new"></div>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:

Reference: https://reactjs.org/docs/react-component.html#getsnapshotbeforeupdate
Similar Reads
ReactJS componentDidUpdate() Method In React, lifecycle methods allow you to manage the behaviour of components at different stages of their existence. One important lifecycle method for handling actions after updates have occurred is componentDidUpdate(). This method is called immediately after a componentâs updates are applied to th
5 min read
ReactJS UNSAFE_componentWillUpdate() Method The componentWillUpdate() method provides us the control to manipulate our React component just before it receives new props or state values. It is called just before the rendering of our component during the updating phase of the React Life-cycle ,i.e., this method gets triggered after the updation
3 min read
ReactJS componentDidMount() Method In React, componentDidMount() is a lifecycle method in React that is called once a component has been rendered and placed in the DOM. This method is invoked only once during the lifecycle of a component, immediately after the first render, which is why it is useful for operations like fetching data,
7 min read
ReactJS componentWillUnmount() Method In React, lifecycle methods allow you to manage the behaviour of components at different stages of their existence. One important lifecycle method for cleaning up resources and side effects is componentWillUnmount(). This method is called just before a component is removed from the DOM, making it an
5 min read
What is onBeforeInputCapture Event in ReactJS ? onBeforeInputCapture Event in ReactJS is an event handler that gets triggered when we make any modifications to the input file, like onBeforeInput, but the difference is that onBeforeInputCapture acts in the capture phase whereas onBeforeInput acts in the bubbling phase i,e. phases of an event. Prer
2 min read
ReactJS UNSAFE_componentWillMount() Method The componentWillMount() method invokes right before our React component gets loaded or mounted in the DOM (Document Object Model). It is called during the mounting phase of the React Life-cycle, i.e., before render(). It is used to fetch data from outside the component by executing the React code s
3 min read