LifeCycle Phase
LifeCycle Phase
LifeCycle Phase
const MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
points: 0
}
this.handlePoints = this.handlePoints.bind(this)
}
}
static getDerivedStateFromProps(props, state) {
return {
points: 200 // update state with this
}
}
static getDerivedStateFromProps(props, state) {
return null
}
Essentially, this method allows a component to update its internal state in response to a change in props.
class App extends Component {
state = {
points: 10
}
// *******
// NB: Not the recommended way to use this method.
// Just an example. Unconditionally overriding state here is generally considered a bad idea
// ********
static getDerivedStateFromProps(props, state) {
return {
points: 1000
}
}
class MyComponent extends React.Component {
render() {
return [
<div key="1">Hello</div>,
<div key="2" >World</div>
];
}
}
// e.g requestAnimationFrame
componentDidMount() {
window.
requestAnimationFrame(this._updateCountdown);
}
// e.g event listeners
componentDidMount() {
el.addEventListener()
}