Skip to content

Instantly share code, notes, and snippets.

@berzniz
Last active March 28, 2018 20:09
Show Gist options
  • Save berzniz/949a9636b13fae0237b9587d8b8f29a4 to your computer and use it in GitHub Desktop.
Save berzniz/949a9636b13fae0237b9587d8b8f29a4 to your computer and use it in GitHub Desktop.
Example for "Update on Async Rendering"
class Example extends React.Component {
componentDidMount () {
this.fetchData(this.props)
}
componentWillReceiveProps (props) {
if (props.id !== this.props.id) {
this.fetchData(props)
}
}
fetchData (props) {
... fetch and store in a Redux store ...
}
render () { ...}
}
@gaearon
Copy link

gaearon commented Mar 28, 2018

class Example extends React.Component {
  componentDidMount () {
    this.fetchData(this.props)
  }

  componentDidUpdate (prevProps) {
    if (prevProps.id !== this.props.id) {
      this.fetchData(this.props)
    }
  }
  
  fetchData (props) {
    ... fetch and store in a Redux store ...
  }
  
  render () { ...}
}

@berzniz
Copy link
Author

berzniz commented Mar 28, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment