Created
July 22, 2019 23:42
-
-
Save victorbruce/3db9f99c569379d8ea54c593f69470d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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