Skip to content

Instantly share code, notes, and snippets.

@victorbruce
Created July 22, 2019 23:42
Show Gist options
  • Save victorbruce/3db9f99c569379d8ea54c593f69470d6 to your computer and use it in GitHub Desktop.
Save victorbruce/3db9f99c569379d8ea54c593f69470d6 to your computer and use it in GitHub Desktop.
// this is just the happy paths
/* PRODUCT COMPONENT */
class Product extends Component {
state = {
products: PRODUCTS,
editProduct: {} // I added a property to the component state
}
// this method gets called when edit button is clicked
handleEdit = (product) => {
this.setState({editProduct: product})
}
render() {
<ProductForm editProduct={editProduct} />
}
}
/* PRODUCT FORM COMPONENT */
class ProductForm extends Component {
// the most important part of the code
// I did not use componentWillReceiveProps() because it is deprecated
componentDidUpdate(prevProps) {
if (prevProps.editProduct !== this.props.editProduct) {
this.setState({product: this.props.editProduct})
}
}
render() {
// code ....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment