I'm using react-select in my project and I'm using it within a map
like that:
renderItems() {
this.props.items.map(item => (
<Select
id="options"
value={this.state.optionSelected}
onChange={this.onChangeOption}
options={this.showOptions()}
/>
);
}
It show correctly all my options for all my items but now I can not get rid about the select...
Basically when I select an option on a single item, that option is changing for all items...
This is what i did so far:
onChangeOption(e) {
this.setState({ optionSelected: e.value });
}
How can I adjust it to change the option only on the one I wish to change it?
Thanks
You are using the same change handler for all of your select components and then set the same state value for all your select components. To deal with this either you need to separate your select components with a container component that handles their own state and change event or you need to give each select component a unique state value.
Example
Try cloning the object to a new object or if this
optionSelected
is a class, you can implement a constructor that clones it for your like:or even
Instead of making
optionSelected
string variable, make it as array in state. Now do the following.What you were doing is using a single variable to hold values of the select box. So if anyone changes, it will reflect all select box