Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 34652  / 8 Years ago, sat, november 26, 2016, 12:00:00

I'm trying to set react-select component for simple select with multiple options but i can't get it working even though it's everything set as docs say. When multi is false, Select works as intended with one value at a time, but when i set multi={true} it shows value as undefined.



When i give in handleChange() event.target.value it gives undefined aswell so thats why i've just used event.value to grab obj property. I'm still newbie to React so any tips about state would be appreciated if i'm doing something wrong here -_-



class StatisticsFilter extends Component {
constructor(props) {
super(props);
this.state = {value: ''};

this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState(event.value);
}

const options =
[
{
value: 'foo', label: 'Foo'
},
{
value: 'bar', label: 'Bar'
},
{
value: 'baz', label: 'Baz'
}
];

render() {
return (
<Select
value={this.state.value}
name=filter__statistics
options={options}
onChange={this.handleChange}
multi={true}
/>
);
}
}


Using react-select v. 1.0.0rc.


More From » reactjs

 Answers
32

There seems to be a few problems with your code. Firstly, the onChange callback will be passed in the value directly instead of the event. Secondly, an object has to be passed to setState.



Could you try changing your handleChange method to the following instead?



handleChange(value) {
this.setState({ value });
}


You can also follow the example code for the Multiselect usage here.


[#59912] Wednesday, November 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
averyc

Total Points: 102
Total Questions: 113
Total Answers: 89

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
;