Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactjs every time refreshing page on setState

Tags:

reactjs

I'm using MultiSelect Plugin for Select DropDown. Here onChange event I'm storing the value in setState. Here is below code :

$('#selectbox-id-onChange').multiselect({
    onChange: function(option, checked) {
         var newArray = this.state.options.slice();
         newArray.push($(option).val());
         this.setState({options: newArray});           
    }
});

The above code is working fine, I'm able to store the array value in state also. But the only issue is that page is refreshing everytime while storing the value in state.

Please do let me know where I'm going wrong here. I tried to figure out but couldn't get the solution.

like image 528
David Avatar asked Jan 23 '26 05:01

David


1 Answers

Yes, that's what React is meant to do. If you do not want it not to re-render then you have to override the shouldComponentUpdate method.

shouldComponentUpdate() {
    return false; // Will cause component to never re-render.
}
like image 162
Alex.Gunning Avatar answered Jan 24 '26 23:01

Alex.Gunning