Skip to content

Instantly share code, notes, and snippets.

@JimmyLv
Created January 7, 2021 11:55
Show Gist options
  • Select an option

  • Save JimmyLv/e0a2517dafe198392f90707475451671 to your computer and use it in GitHub Desktop.

Select an option

Save JimmyLv/e0a2517dafe198392f90707475451671 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
class App extends React.Component {
constructor() {
super()
this.state = {
m: 0
}
}
static getDerivedStateFromProps(nextProps, prevState) {
console.log("getDerived", nextProps, prevState);
if (nextProps.n !== prevState.m) {
// 这里 return 的对象相当于 setState 了
return {
m: nextProps.n
}
}
return null
}
render() {
return (
<div>{m}</div> )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment