The react hook equivalent to the old componentWillReceive props can be done using the useEffect hook, just specifying the prop that we want to listen for changes in the dependency array.

I.e:

export default (props) => {

    useEffect( () => {
        console.log('counter updated');
    }, [props.counter])

    return <div>Hi {props.counter}</div>
}

For componentDidUpdate just by omitting the dependency array, the useEffect function will be called after every re-render.

I.e:

export default (props) => {

    useEffect( () => {
        console.log('counter updated');
    })

    return <div>Hi {props.counter}</div>
}
Answer from fgonzalez on Stack Overflow
🌐
React
react.dev › reference › react › Component
Component – React
Calling setState inside ... component. If you define UNSAFE_componentWillReceiveProps, React will call it when the component receives new props....
🌐
Medium
medium.com › @tomaszkudliski › componentwillreceiveprops-to-react-hook-bda7e261680a
componentWillReceiveProps in React Hook | by Tomasz Kudlinski | Medium
May 9, 2020 - Sometimes logic inside of UNSAFE_componentWillReceiveProps function will check the current and next value of the propA, like here:
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › what-are-the-alternatives-to-componentwillreceiveprops-in-react
What Are The Alternatives To componentWillReceiveProps In React? - GeeksforGeeks
July 23, 2025 - In React, componentWillReceiveProps is a lifecycle method that was deprecated in version 16.3 and subsequently removed in version 17.
🌐
Gitbooks
developmentarc.gitbooks.io › react-indepth › content › life_cycle › update › component_will_receive_props.html
Updating and componentWillReceiveProps() · react-indepth
Now that we have discussed starting an Update, let's dive into the Update life cycle methods. The first method available to us is componentWillReceiveProps(). This method is called when props are passed to the Component instance.
🌐
GitHub
gist.github.com › WrathChaos › c476aac7c102ea2f01fd955c05b685f8
React Hooks: componentWillReceiveProps · GitHub
Save WrathChaos/c476aac7c102ea2f01fd955c05b685f8 to your computer and use it in GitHub Desktop. ... // Class componentWillReceiveProps(nextProps) { if (nextProps.data !== this.props.data) { console.log('Prop Received: ', nextProps.data); } } // React Hooks: componentWillReceiveProps useEffect(() => { console.log('Prop Received: ', props.data); }, [props.data])
🌐
DEV Community
dev.to › alexandprivate › nextprops-in-react-functional-components-1jc2
Compare Props in React Functional Components. - DEV Community
April 29, 2021 - componentWillReceiveProps(nextProps) { if(nextProps.count !== this.props.count) { // Do something here since count prop has a new value } } So let's say you need to do something like this in React 17 today, to skip an apollo query or to avoid any kinda side effects inside your components? The first thing that may cross your mind is to set some states inside your component to track the props values using a useEffect hook: function ComponentGettingProps({count, ...restProps}) { const [localCount, setLocalCount] = React.useState(0) React.useEffect(() => { if(count === localCount) { // count prop has the same value setLocalCount(count) // ...
Find elsewhere
🌐
Quora
quora.com › How-do-you-use-ComponentWillReceiveProps-and-componentDidUpdate-for-a-React-Hook
How to use 'ComponentWillReceiveProps' and 'componentDidUpdate' for a React Hook - Quora
Answer: You need to use [code]useEffect [/code]in order to use ComponentWillReceiveProps and ComponentDidUpdate lifecycle methods. The syntax of useEffect hook is something like this > [code]useEffect(()=>{},[prop]) [/code] The first param in the above syntax(that is the function call) is the...
🌐
Bitstack
blog.bitsrc.io › replacing-react-lifecycle-methods-with-hooks-a-comprehensive-guide-dfd5cbe1f274
Replacing React Lifecycle Methods with Hooks: A Comprehensive Guide | by John Au-Yeung | Bits and Pieces
May 4, 2020 - Therefore, we have clearInterval in that function to remove the timer from when the component unmounts. The useEffect hook is also the equivalent of the componentWillReceiveProps or componentDidUpdate hooks.
🌐
TutorialsPoint
tutorialspoint.com › home › articles on trending technologies › reactjs – componentwillreceiveprops() method
ReactJS – componentWillReceiveProps() Method
March 18, 2021 - This function is generally called if the props passed to the component change. It is used to update the state in response with the new received props. setState() method doesn’t generally call this method again.
🌐
DevGenius
blog.devgenius.io › react-lifecycle-methods-transitioning-from-class-components-to-functional-components-778b07461fb0
React Lifecycle Methods: Transitioning from Class components to Functional Components | by Wanuja Ranasinghe | Dev Genius
March 15, 2024 - // Class Component Example: class UserProfile extends React.Component { componentWillReceiveProps(nextProps) { if (this.props.userId !== nextProps.userId) { this.fetchUserData(nextProps.userId); } } render() { // Render user profile } } // Functional Component Equivalent: function UserProfile({ userId }) { useEffect(() => { // Fetch user data when userId changes fetchUserData(userId); }, [userId]); // Render user profile } 4.
🌐
Rheinwerk Computing
blog.rheinwerk-computing.com › the-component-lifecycle-in-react
The Component Lifecycle in React
March 19, 2025 - componentWillReceiveProps: The componentWillReceiveProps method is called before the props are updated.
Top answer
1 of 5
81

componentWillReceiveProps is required if you want to update the state values with new props values, this method will get called whenever any change happens to props values.


In your case why you need this componentWillReceiveProps method?

Because you are storing the props values in state variable, and using it like this:

this.state.KeyName

That's why you need componentWillReceiveProps lifecycle method to update the state value with new props value, only props values of component will get updated but automatically state will not get updated. If you do not update the state then this.state will always have the initial data.

componentWillReceiveProps will be not required if you do not store the props values in state and directly use:

this.props.keyName

Now react will always use updated props values inside render method, and if any change happen to props, it will re-render the component with new props.

As per DOC:

componentWillReceiveProps() is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this method.

React doesn't call componentWillReceiveProps with initial props during mounting. It only calls this method if some of component's props may update.

Suggestion:

Do not store the props values in state, directly use this.props and create the ui components.

2 of 5
20

Update

componentDidUpdate()

should now be used rather than componentWillReceiveProps

also see an article from gaearon re writing resilient components

There are two potential issues here

  1. Don't reassign your props to state that is what you are using redux for pulling the values from the store and returning them as props to your component

Avoiding state means you no longer need your constructor or life-cycle methods. So your component can be written as a stateless functional component there are performance benefits to writing your component in this way.

  1. You do not need to wrap your action in dispatch is you are passing mapDispatcahToProps. If an object is passed, each function inside it is assumed to be a action creator. An object with the same function names, but with every action creator wrapped into a dispatch will be returned

Below is a code snippet that removes the state from your component and relies on the state that has been returned from the redux store

import React from "react";
import { connect } from "react-redux";

const TrajectContainer = ({ trajects, addTraject }) => (
	<div className="col-md-6">
		<h2>Trajects</h2>
		<button className="btn btn-primary" onClick={addTraject}>Add new Traject</button>
		{trajects.map(traject => <Traject traject={traject} key={traject.name} />)}
	</div>
);

const mapStateToProps = ({ trajects }) => ({ trajects });

export default connect( mapStateToProps, { addTraject })(TrajectContainer);

🌐
React
legacy.reactjs.org › docs › react-component.html
React.Component – React
If you’d like, you can reuse ... extracting pure functions of the component props and state outside the class definition. Note that this method is fired on every render, regardless of the cause. This is in contrast to UNSAFE_componentWillReceiveProps, which only fires when ...
🌐
DhiWise
dhiwise.com › post › how-does-componentwillreceiveprops-work-in-react
componentWillReceiveProps in React
November 21, 2024 - In this example, the ErrorBoundary component catches errors and displays a fallback UI, ensuring the rest of the application remains functional. Avoid using componentWillReceiveProps for updating state or performing side effects. Instead, use getDerivedStateFromProps or componentDidUpdate to handle these tasks safely.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › reactjs-unsafe_componentwillreceiveprops-method
ReactJS UNSAFE_componentWillReceiveProps() Method - GeeksforGeeks
July 27, 2025 - Explanation: We create a state of the count with an initial value of 0 and a function handleCount for incrementing its current value by 1 in our App class component. The latter gets triggered as an · onClick event when we click on our button element. Our state value increments when we click on the button and this updated value is passed as props to another class component PropComponent. Before our PropComponent receives new props, it logs the updated props values on the console through UNSAFE_componentWillReceiveProps(newProps) method.
🌐
ITNEXT
itnext.io › react17-or-how-to-get-rid-of-componentwillreceiveprops-c91f9a6f6f03
React17, or how to get rid of “componentWillReceiveProps”? | by Giedrius Vičkus | ITNEXT
January 4, 2021 - This method doesn’t have access to the component instance. If you’d like, you can reuse some code between getDerivedStateFromProps() and the other class methods by extracting pure functions of the component props and state outside the class definition.
🌐
egghead.io
egghead.io › lessons › react-refactor-componentwillreceiveprops-to-getderivedstatefromprops-in-react-16-3
Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3 | egghead.io
[00:47] The response is stored in a state property called Computed Message. This function is used in componentDidMount to retrieve an initial message and in componentWillReceiveProps to update when props change, which brings us to the second thing the box component does.
Published   April 19, 2018
🌐
DigitalOcean
digitalocean.com › community › tutorials › react-lifecycle-functions
Component Lifecycle Functions in React | DigitalOcean
May 13, 2017 - class Scorecard extends Component { // Other functions omitted for brevity. componentWillReceiveProps(nextProps) { // `nextProps` is the new props, while `this.props` are the old ones. const {playerName} = this.props; // It is entirely possible that the new `playerName` is the same as the old one.