I am trying to call a static method from within another static method in a React component:
class HelloWorld extends React.Component {
static add(a, b){
return a + b;
}
static getDerivedStateFromProps(props, state){
const sum = this.add(2, 2);
return {
sum
}
}
render() {
return <div>Hello World</div>
}
}
Live demo: https://codesandbox.io/s/rmxy909ovo
But I get the error that this is undefined, even though MDN says:
In order to call a static method within another static method of the same class, you can use the this keyword.
Why is this in a static method undefined and how to call the method add within getDerivedStateFromProps in this example?
HelloWorld.add()(it does work in the browser, apparently not for transpiled ES6 though)