question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this is undefined in getDerivedStateFromProps

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? this is undefined in getDerivedStateFromProps

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

class MyComponent extends Component {
  static sayHello() {
     console.log('Hello')
  }

  static getDerivedStateFromProps() {
     this.sayHello()
  }
}

What is the expected behavior? I would expect the above to work. To me it is unexpected that a static method is not getting called on the class.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 16.5.1

This seems like an easy fix to me, here

Is there a reason not to change line 157 and 161?

// From this
getDerivedStateFromProps.(nextProps, prevState);

// To this?
getDerivedStateFromProps.call(ctor, nextProps, prevState);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jquensecommented, Jan 30, 2019

this in a static method can be the constructor (not the instance)

class Foo { static bar() { console.log(this.baz) } static baz(){} }
3reactions
milesjcommented, Jan 30, 2019

To me it is unexpected that a static method is not getting called on the class.

Outside of what @TrySound has linked, this is how JS works. Static methods do not have access to a class instance’s this, and IMO, it never should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getDerivedStateFromProps returned undefined - Stack Overflow
The getDerivedStateFromProps method should return the updated slice of state, instead of updating the state object passed as an argument.
Read more >
React.js static getDerivedStateFromProps() - GeeksforGeeks
getDerivedStateFromProps (props, state) is a static method that is called just before render() method in both mounting and updating phase in ...
Read more >
React.Component
static getDerivedStateFromProps(); shouldComponentUpdate(); render() ... Otherwise, this.props will be undefined in the constructor, which can lead to bugs.
Read more >
[Solved]-getDerivedStateFromProps returned undefined-Reactjs
Coding example for the question getDerivedStateFromProps returned undefined-Reactjs.
Read more >
Debugging 'TypeError: Cannot read property 'value' of ...
Debugging 'TypeError: Cannot read property 'value' of undefined' ... After the constructor is called, static getDerivedStateFromProps gets ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found