DropDownMenu-Maximum Call Stack Exceeded when using setState() in OnOptionSelected
Trying to setState() in onOptionSelected throws Maximum Call Stack Exceeded. I've tried it numerous ways but keep running into the same issue. I can console.log() inside the arrow function so its for sure related to setState().
Also entirely possible that this is a problem with my code lifecycle. Please advise
var DDmenu = React.createClass({
getInitialState: function() {
return {
category: ''
};
},
render(){
return(
<DropDownMenu
onOptionSelected={(option) => this.handleOptSelect(option)}
options={[
{
name: 'Cleaning',
id: 'cleaning',
},
{
name: 'Construction',
id: 'construction',
}
]}
titleProperty={'name'}
valueProperty={'id'}
selectedOption={ this.state.category }
/>
);
},
handleOptSelect: function(option){
this.setState({
category: option.id
}),
function(){
console.log(this.state.category);
};
}
});
Having this same issue. It appears that the onSelectOption is firing at render which is triggering the rerender.
DropDownMenu has been changed in v0.10.0. Now it requires selectedOption prop, so, according to it your code should look like this:
var options = [{
name: 'Cleaning',
id: 'cleaning',
}, {
name: 'Construction',
id: 'construction',
}];
var DDmenu = React.createClass({
getInitialState: function() {
return {
category: options[0]
};
},
render(){
return(
<DropDownMenu
onOptionSelected={(option) => this.handleOptSelect(option)}
selectedOption={this.state.category}
options={options}
titleProperty={'name'}
valueProperty={'id'}
selectedOption={ this.state.category }
/>
);
},
handleOptSelect: function(option){
this.setState({
category: option
}),
function(){
console.log(this.state.category);
};
}
We need to make sure this is documented