ui icon indicating copy to clipboard operation
ui copied to clipboard

DropDownMenu-Maximum Call Stack Exceeded when using setState() in OnOptionSelected

Open jcd1991 opened this issue 10 years ago • 3 comments

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);
       };
  }
});

jcd1991 avatar Oct 12 '16 16:10 jcd1991

Having this same issue. It appears that the onSelectOption is firing at render which is triggering the rerender.

opula avatar Oct 24 '16 10:10 opula

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);
       };
  }

SoHotSoup avatar Dec 07 '16 15:12 SoHotSoup

We need to make sure this is documented

tenodi avatar Apr 03 '17 10:04 tenodi