I have a global variable
const initialProductState = {
id: 0,
name: '',
price: 0,
rating: 0
}
I am using it to set state in my Products component as
state = {
product : initialProductState
}
And when I am calling a method(addProduct
) which is using initialState to set the current state to initial from another component(ProductDetail
), it unexpectedly updates initialProductState. I am unable to find the reason for this update.
Here's my code :
Products component :
class Products extends Component {
constructor(props){
super(props);
this.state = {
product: initialProductState,
}
}
addProduct = (product) => {
const newProduct = {
...product,
id : this.state.totalProducts + 1,
}
console.log(" initialProductState ", initialProductState)
this.setState({
products : newProductsArray,
totalProducts : this.state.totalProducts + 1,
product : initialProductState
})
}
}
render(){
return
<ProductDetail
product={this.state.product}
addProduct={this.addProduct}
saveProduct={this.saveProduct}
deleteProduct={this.deleteProduct}
cancelProduct={this.cancelProduct}
/>
}
ProductDetail component :
class ProductDetail extends React.Component {
state = {
product: initialstate,
priceError: "",
nameError: "",
ratingError: ""
}
static getDerivedStateFromProps(props, state) {
console.log("inside getDerivedStateFromProps")
if (props.product !== state.product) {
return {
product: props.product
};
}
// Return null to indicate no change to state.
return null;
}
render() {
return (
<Button
variant="contained"
color="primary"
disabled = {!(this.props.product.id === 0)}
onClick={() => {
this.props.addProduct({
name: 'random',
price: 10,
rating: 5
})}
}
>
Add
</Button>
)
}
}