You should check the docs

You can have a state for the previous path and the current path and only call setLoginStatus when they are different.

If you only want to setLoginStatus(true) when path == 'login' then using the useEffect is correct.

This part of the code will only run when path changes. And this happens because of [ path ].

useEffect(() => {
    if (path ==='/login'){
        setLoginStatus(true)
    } else {
    setLoginStatus(false)
    }
}, [ path ])

And if you are using some router, probably your effect will run only in the initial render.

I'm just wondering if I'm not losing anything by converting to this.

No, it's fine, that's the way you should do it

Also take a look at You Probably Don't Need Derived State

And here

Answer from Vencovsky on Stack Overflow
🌐
Medium
medium.com › swlh › lifecycle-state-and-getderivedstatefromprops-761b3a19c4e
Lifecycle, state, getDerivedStateFromProps and Hooks | by Thomas Rubattel | The Startup | Medium | The Startup
December 17, 2022 - Within getDerivedStateFromProps in IframeWithSpinner we cannot access to its current url props, since this lifecycle function has no access to the this context. So we set an additional state for keeping track of it, that we called prevUrl. import React from "react"; import isURL from "validator/lib/isURL"; import spinner from "./ring-loader.gif"; class Iframe extends React.Component { handleOnLoad = (e) => this.props.onLoad ?
Discussions

reactjs - How to sync props to state using React hooks : setState() - Stack Overflow
Wrong answer, this will result in a wasteful, ghost re-rendering. useEffect has no business being used here. The simplest way to do it is: reactjs.org/docs/… There are more advanced techniques applicable in some cases, like coding a useComputedState hook. Some other suggestions: ... More on stackoverflow.com
🌐 stackoverflow.com
Newest 'getderivedstatefromprops' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
Hooks refactoring of getDerivedStateFromProps/componentDidUpdate and shouldComponentUpdate to prevent re-rendering
Hello there, I have a use case which I am not sure can completely be covered by the hooks API, but has come up several times. It is similar to #808 except there are two modifications: 1) the fact i... More on github.com
🌐 github.com
0
December 12, 2019
How to use lifecycle method getDerivedStateFromProps as opposed to componentWillReceiveProps
It looks like componentWillReceiveProps is going to be completely phased out in coming releases, in favor of a new lifecycle method getDerivedStateFromProps:static getDerivedStateFromProps(). Upon More on stackoverflow.com
🌐 stackoverflow.com
🌐
React
legacy.reactjs.org › docs › hooks-faq.html
Hooks FAQ – React
getDerivedStateFromProps: Schedule an update while rendering instead. ... componentDidMount, componentDidUpdate, componentWillUnmount: The useEffect Hook can express all combinations of these (including less common cases). getSnapshotBeforeUpdate, ...
Top answer
1 of 8
166

useState hooks function argument is being used only once and not everytime the prop changes. You must make use of useEffect hooks to implement what you would call the componentWillReceiveProps/getDerivedStateFromProps functionality

import React,{useState , useEffect} from 'react';

const Persons = (props) =>  {
   const [nameState , setNameState] = useState(props)

   useEffect(() => {
       setNameState(props);
   }, [props])

   return (
            <div>
                <p>My name is {props.name} and my age is {props.age}</p>
                <p>My profession is {props.profession}</p>
            </div>
        )

}

export default Persons;
2 of 8
41

The props value in useState(props) is used only during the initial render, further state updates are done with the setter setNameState.

In addition, there is no need for useEffect when updating derived state:

const Person = props => {
  const [nameState, setNameState] = useState(props.name);
  // update derived state conditionally without useEffect
  if (props.name !== nameState) setNameState(props.name);
  // ... other render code
};

From React docs:

[...] you can update the state right during rendering. React will re-run the component with updated state immediately after exiting the first render so it wouldn’t be expensive.

[...] an update during rendering is exactly what getDerivedStateFromProps has always been like conceptually.

In essence, we can optimize performance by getting rid of an additional browser repaint phase, as useEffect always runs after the render is committed to the screen.

Working example

This is a contrived example illustrating above pattern - in real code you would read props.name directly. See the React blog post for more appropriate derived state use cases.

const Person = props => {
  const [nameState, setNameState] = React.useState(props.name);
  // Here, we update derived state without useEffect
  if (props.name !== nameState) setNameState(props.name);

  return (
    <p>
      <h3>Person</h3>
      <div>{nameState} (from derived state)</div>
      <div>{props.name} (from props)</div>
      <p>Note: Derived state is synchronized/contains same value as props.name</p>
    </p>
  );
};

const App = () => {
  const [personName, setPersonName] = React.useState("Lui");
  const changeName = () => setPersonName(personName === "Lukas" ? "Lui" : "Lukas");

  return (
    <div>
      <Person name={personName} />
      <button onClick={changeName}>Change props</button>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js" integrity="sha256-32Gmw5rBDXyMjg/73FgpukoTZdMrxuYW7tj8adbN8z4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js" integrity="sha256-bjQ42ac3EN0GqK40pC9gGi/YixvKyZ24qMP/9HiGW7w=" crossorigin="anonymous"></script>
<div id="root"></div>

🌐
Irigoyen
irigoyen.dev › blog › 2021 › 04 › 07 › converting-react-class-component-lifecycle-methods-to-hooks
Converting React Class Component Lifecycle Methods to Hooks by Michael Irigoyen
April 7, 2021 - While it is unlikely that you will need it, you can simply update the state during render. React will run the component again immediately after the first render with the updated state. While this may seem like the wrong way to handle this, an update during rendering is how getDerivedStateFromProps has always worked under the hood.
🌐
DhiWise
dhiwise.com › post › best-practices-for-using-getderivedstatefromprops-in-your-react-applications
The Role of getDerivedStateFromProps in the React
October 27, 2023 - The useEffect hook is then used to update the derivedData state whenever the data prop changes. In React, state can be passed as props from a parent component to a child component.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-js-static-getderivedstatefromprops
React.js static getDerivedStateFromProps() - GeeksforGeeks
July 23, 2025 - React Hooks · React Router · ... Report · The getDerivedStateFromProps method is a static lifecycle method used when the state of a component depends on changes of props....
Find elsewhere
🌐
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. To handle similar logic, you can use alternative lifecycle methods and hooks that are recommended in modern React development. This article will explore the recommended alternatives: getDerivedStateFromProps, componentDidUpdate, and hooks like useEffect.
🌐
GitHub
github.com › rohan-paul › Awesome-JavaScript-Interviews › blob › master › React › Hooks › useState-replace-componentWillReceiveProps-getDerivedStateFromProps.md
Awesome-JavaScript-Interviews/React/Hooks/useState-replace-componentWillReceiveProps-getDerivedStateFromProps.md at master · rohan-paul/Awesome-JavaScript-Interviews
Look below example taken from an SO question for the right way to do this. You must make use of useEffect hooks to implement what you would call the componentWillReceiveProps/getDerivedStateFromProps functionality.
Author   rohan-paul
🌐
React
react.dev › reference › react › Component
Component – React
There is no direct equivalent for static getDerivedStateFromError in function components yet. If you’d like to avoid creating class components, write a single ErrorBoundary component like above and use it throughout your app.
🌐
DEV Community
dev.to › dristy03 › bridge-between-lifecycle-methods-hooks-54j7
Bridge Between Lifecycle Methods & Hooks - DEV Community
April 23, 2023 - ... Many other methods can be implemented using 2 hooks together; like getDerivedStateFromProps() can be achieved using useState() and useEffect(), getSnapshotBeforeUpdate() can be achieved using useState() and useLayoutEffect() and so on.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › getderivedstatefromprops
Newest 'getderivedstatefromprops' Questions - Stack Overflow
Today we can use getDerivedStateFromProps for class components and state updates during render phase for React Hooks/function components to create derived state, if needed.
🌐
DigitalOcean
digitalocean.com › community › tutorials › react-get-derived-state
Using Derived State in React | DigitalOcean
July 1, 2018 - class List extends React.Component { static getDerivedStateFromProps(props, state) { if (props.selected !== state.selected) { return { selected: props.selected, }; } // Return null if the state hasn't changed return null; } // ...
🌐
GitHub
github.com › reactjs › react.dev › issues › 2611
Hooks refactoring of getDerivedStateFromProps/componentDidUpdate and shouldComponentUpdate to prevent re-rendering · Issue #2611 · reactjs/react.dev
December 12, 2019 - It is similar to #808 except there are two modifications: 1) the fact it is a hooks implementation, rather than classy; and 2) I need to pass in additional props from the parent component down to the component concerned and avoid a re-render. When using other DOM manipulation libraries and classes that are positioned beneath the lowest most React-created DOM nodes, one may wish to avoid any re-rendering of the React wrapping component.
Published   Dec 12, 2019
🌐
TutorialsPoint
tutorialspoint.com › home › reactjs › reactjs getderivedstatefromprops method
ReactJS - static getDerivedStateFromProps() Method
February 13, 2026 - Learn about the getDerivedStateFromProps method in ReactJS, its usage, and how it helps manage state updates effectively.
Top answer
1 of 4
119

About the removal of componentWillReceiveProps: you should be able to handle its uses with a combination of getDerivedStateFromProps and componentDidUpdate, see the React blog post for example migrations. And yes, the object returned by getDerivedStateFromProps updates the state similarly to an object passed to setState.

In case you really need the old value of a prop, you can always cache it in your state with something like this:

state = {
  cachedSomeProp: null
  // ... rest of initial state
};

static getDerivedStateFromProps(nextProps, prevState) {
  // do things with nextProps.someProp and prevState.cachedSomeProp
  return {
    cachedSomeProp: nextProps.someProp,
    // ... other derived state properties
  };
}

Anything that doesn't affect the state can be put in componentDidUpdate, and there's even a getSnapshotBeforeUpdate for very low-level stuff.

UPDATE: To get a feel for the new (and old) lifecycle methods, the react-lifecycle-visualizer package may be helpful.

2 of 4
63

As we recently posted on the React blog, in the vast majority of cases you don't need getDerivedStateFromProps at all.

If you just want to compute some derived data, either:

  1. Do it right inside render
  2. Or, if re-calculating it is expensive, use a memoization helper like memoize-one.

Here's the simplest "after" example:

import memoize from "memoize-one";

class ExampleComponent extends React.Component {
  getDerivedData = memoize(computeDerivedState);

  render() {
    const derivedData = this.getDerivedData(this.props.someValue);
    // ...
  }
}

Check out this section of the blog post to learn more.

🌐
egghead.io
egghead.io › lessons › react-update-state-based-on-props-using-the-lifecycle-hook-getderivedstatefromprops-in-react16-3
Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3 | egghead.io
getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement for componentWillReceiveProps. It is invoked after a component is instantiated as well as when it receives new props.
Published   April 9, 2018
🌐
Substack
hayksimonyan.substack.com › p › react-lifecycle-methods-and-their
React Lifecycle Methods and Their Equivalent Hooks
November 1, 2023 - React now promotes the use of functional components as they offer a simpler and more concise way of writing components. All the lifecycle hooks are available in functional components as well. It’s worth mentioning that there are other lifecycle hooks, that we haven’t discussed in this tutorial (such as getDerivedStateFromProps, getSnapshotBeforeUpdate, etc).
🌐
Larry-price
larry-price.com › blog › 2018 › 06 › 27 › how-to-use-getderivedstatefromprops-in-react-16-dot-3-plus
How to Use getDerivedStateFromProps in React 16.3+ - Larry Price
As opposed to getDerivedStateFromProps, we have access to the context provided by this. Note that this method also has arguments for prevProps and prevState, which provides the previous versions of the component’s props and state for comparison to the current values. The deprecation of these lifecycle methods won’t happen until React 17, but it’s always good to plan ahead.