Search code examples
reactjslifecycle

Call a method in getDerivedStateFromProps


Currently my code is like so:

componentWillReceiveProps(nextProps: any) {
  this.showLockedWarning(nextProps);
}

but due to the componentWillReceiveProps getting deprecated, I'd like to use the function that has no such problems and as far as I see, I should have 2 options - getDerivedStateFromProps and componentDidUpdate.

I can't use componentDidUpdate because that creates an infinite loop, but from getDerivedStateFromProps I can't access this keyword. What could I do in this case?


Solution

  • If you're not changing state (as in the example), then it would be better to move the code to componentDidUpdate(), but if you need to change state and call a function, then you could make the function you want to call static as well.