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 - Lifecycle, state, getDerivedStateFromProps and Hooks Introduction React 16.3 introduced a new lifecycle function called getDerivedStateFromProps. This new lifecycle function is meant as replacement …
🌐
React
legacy.reactjs.org › docs › hooks-faq.html
Hooks FAQ – React
You can initialize the state in the useState call. If computing the initial state is expensive, you can pass a function to useState. getDerivedStateFromProps: Schedule an update while rendering instead.
🌐
DhiWise
dhiwise.com › post › best-practices-for-using-getderivedstatefromprops-in-your-react-applications
The Role of getDerivedStateFromProps in the React
October 27, 2023 - In functional components, you can use the useState and useEffect hooks to achieve similar functionality to getDerivedStateFromProps in class components.
🌐
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 - If you're looking for a better understanding on the React lifecycle using Hooks, I hope that this is a useful reference. constructor() render() componentDidMount() componentWillUnmount() componentDidUpdate() getDerivedStateFromProps() shouldComponentUpdate() UNSAFE_componentWillMount() UNSAFE_componentWillUpdate() UNSAFE_componentWillReceiveProps() getSnapshotBeforeUpdate() getDerivedStateFromError() componentDidCatch() Functional Components don't require a constructor.
🌐
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 - Hooks refactoring of getDerivedStateFromProps/componentDidUpdate and shouldComponentUpdate to prevent re-rendering#2611
Published   Dec 12, 2019
🌐
Netlify
niharraoteblog.netlify.app › creation-lifecycle-of-a-react-component
Creation lifecycle of a class-based React component
It is invoked right before the render function does. getDerivedStateFromProps is called during the initial mount of the component and on subsequent updates. It is a rarely used lifecycle hook, for when the state of our component has to be derived from changes made in props over time.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-js-static-getderivedstatefromprops
React.js static getDerivedStateFromProps() - GeeksforGeeks
July 23, 2025 - // Filename - App.js import React from "react"; import ReactDOM from "react-dom"; class App extends React.Component { render() { return ( <div> <Child name="sachin"></Child> </div> ); } } class Child extends React.Component { constructor(props) { super(props); this.state = { name: "kapil" }; } static getDerivedStateFromProps(props, state) { if (props.name !== state.name) { //Change in props return { name: props.name }; } return null; // No change to state } /* if props changes then after getDerivedStateFromProps method, state will look something like { name: props.name } */ render() { return <div> My name is {this.state.name}</div>; } } export default App;
Find elsewhere
🌐
YouTube
youtube.com › watch
Reactjs 16 tutorial #24 getDerivedStateFromProps life cycle example - YouTube
in this react js tutorial we learn how to use of getDerivedStateFromProps or get Derived State From Props life cycle method and why use it with simple exampl...
Published   August 13, 2019
🌐
Rheinwerk Computing
blog.rheinwerk-computing.com › the-component-lifecycle-in-react
The Component Lifecycle in React
March 19, 2025 - When you return to the browser after this change, you’ll see in the console that the render method is only executed on every other setState. The getDerivedStateFromProps and shouldComponentUpdate methods are still called every second. The getSnapshotBeforeUpdate method enables you to implement a hook that is executed after the render method has been run and before the changes are displayed.
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>

🌐
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
🌐
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
getDerivedStateFromProps is invoked every time a component is rendered. It takes in two arguments: the next props object (which may be the same as the previous object) and the previous state object of the component in question.
🌐
React
react.dev › reference › react › Component
Component – React
If you need to “adjust” some state when a prop changes, check whether you can compute all the necessary information from props alone during rendering. If you can’t, use static getDerivedStateFromProps instead.
🌐
CodeSandbox
codesandbox.io › s › react-getderivedstatefromprops-to-hooks-y9boj
React - getDerivedStateFromProps to hooks - CodeSandbox
October 8, 2019 - React - getDerivedStateFromProps to hooks by net-uk-sweet using react, react-dom, react-scripts
Published   Oct 08, 2019
Author   net-uk-sweet
🌐
DEV Community
dev.to › dristy03 › bridge-between-lifecycle-methods-hooks-54j7
Bridge Between Lifecycle Methods & Hooks - DEV Community
April 23, 2023 - React Hooks were introduced in React 16.8 to bridge the gap between functional and class components, providing state management and lifecycle methods in functional components without the need for classes. There are a number of methods in each phases. If we jot it down together in a picture: We can write this down in the following way: Mounting: constructor() static getDerivedStateFromProps() render() componentDidMount() Updating: static getDerivedStateFromProps() shouldComponentUpdate() render() getSnapshotBeforeUpdate() componentDidUpdate() Unmounting: componentWillUnmount() In addition to these methods, there are a few other methods that can be used to handle errors and update the state of the component.
🌐
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 ... 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 useEffe...
🌐
TutorialsPoint
tutorialspoint.com › home › reactjs › reactjs getderivedstatefromprops method
ReactJS - static getDerivedStateFromProps() Method
February 13, 2026 - In the above example, The Form component has a user ID and an email input. When the user ID changes, the getDerivedStateFromProps function resets the email. The function handleUserIDChange simulates a user ID change. The App component sets the Form and manages changes to the user ID.