Answer the question
In order to leave comments, you need to log in
How to avoid child component's render when parent's state changes?
Good time!
I encountered such a small problem, there was a component with input fields and dropZone , over time, the code grew and I had to create a separate child component with dropZone , in the dropZone itself, files are received and notifications are shown, but one notification and notification clearing occurred when the button from the component was pressed with fields.
Thus, I came to the conclusion that it would be more reasonable in a component with fields to create a state with a number when the button is pressed.
i.e. here is the old function when the button is clicked, and depending on the fact that if we show an error in the state data, or we return our data in the sate to its original state.
takeVal() {
if(this.state.author.length !== 0 && this.state.name.length !== 0 && this.state.imagePreviewUrl){
this.setState({
imgMessageClass: 'dz-message',
imagePreviewUrl: '',
imgStatus: false
});
this.clearInputs();
}else {
this.setState({
imgInfoClass: 'dz-info dz-error',
imageStatusMessage: 'Заполните все поля и загрузите обложку!'
});
setTimeout(()=> this.setState({
imageStatusMessage: '',
imgInfoClass: 'dz-info'
}), 2500);
}
}
if(this.state.author.length !== 0 && this.state.name.length !== 0 && this.state.imagePreviewUrl){
this.setState({
imgNotice: 5
});
this.clearInputs();
}else {
this.setState({
imgNotice: 3
});
}
componentWillReceiveProps (){
if(this.props.imgNotice === 5){
this.setState({
imgMessageClass: 'dz-message',
imagePreviewUrl: '',
imgStatus: false
});
}else if(this.props.imgNotice === 3){
this.setState({
imgInfoClass: 'dz-info dz-error',
imageStatusMessage: 'Заполните все поля и загрузите обложку!'
});
setTimeout(()=> this.setState({
imageStatusMessage: '',
imgInfoClass: 'dz-info'
}), 2500);
}
}
Answer the question
In order to leave comments, you need to log in
A common problem with nested components. The simplest solution in this situation is to use component should update. Another way out is to use pure components.
shouldComponentUpdate on a child element, try applying:
https://facebook.github.io/react/docs/optimizing-p...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question