Select list gets empty on state change
See original GitHub issueI am developing a SPA which has some filters. I am able to add the select list and on change event I am updating the state of the react application.
States
this.state = { genres: null , categories: null , fBooks: [] , gBooks: [] };
React tags
<Select
name="genre"
value=""
options={this.state.genres}
onChange={logChange}
/>
<Select
name="category"
value=""
options={this.state.categories}
multi={true}
onChange={logChange}
/>
logChange
var logChange = (function(val) {
filterBooks(val.split(','));
}).bind(this);
and the filterBooks method
var filterBooks = (function(val){
var selected = [];
_.filter(this.state.gBooks, function (item) {
if(_.contains(val, item.genre.name) === true){
selected.push(item);
}
});
this.setState({fBooks: selected});
console.log(selected);
}).bind(this);
When the select list is first changed, the item are filtered correctly but when I select another option the select list changes to blank. Visuals might help better.
This only happens when I change the state to re-render the list
this.setState({fBooks: selected});
Otherwise it works fine.
Issue Analytics
- State:
- Created 8 years ago
- Comments:11
Top Results From Across the Web
Handle Select Option in React giving me empty string first time
Quoting the react docs: Calling the set function does not change state in the running code ... states behaves like a snapshot.
Read more >Why does AngularJS include an empty option in select
After a value is selected, the drop-down gets back to its default state. However, while working with the AngularJS dropdown, it was observed ......
Read more >Customize a reusable React dropdown menu component
Let's get started! The visual structure of a dropdown menu component; Parent-child relations in dropdown components; Controlling a parent state ...
Read more >Empty state dropdown search reactive app - OutSystems
What I've identified so far is that you have to put something to "Empty Text" attribute of a Dropdown and set corresponding variable...
Read more >Handle the onChange event on a Select element in React
Every time the user changes the selected option, update the state variable. ... In the example, we set the initially selected value to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m using redux-form and the onBlur event used to dispatch empty values which is quite ennoying. I’ts ugly but it works (1.0.0-beta6):
I have the same problem and you should separate the Select tag in a isolated component which will render only if the options props changed.
Something like:
The onChange props is passed by the parent and it will change the parent state without re-rendering the JobRunForm.
If you don’t need the allowCreate, you should try the beta version, it solves the problem more elegantly.