question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select list gets empty on state change

See original GitHub issue

I 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.

screen shot 2015-11-24 at 5 31 48 pm screen shot 2015-11-24 at 5 32 01 pm screen shot 2015-11-24 at 5 32 06 pm screen shot 2015-11-24 at 5 32 10 pm

This only happens when I change the state to re-render the list this.setState({fBooks: selected}); Otherwise it works fine.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
jonathanasquiercommented, Dec 3, 2015

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):

<Select
     options={options}
    {...field}
     onBlur={function(){}}
 />
1reaction
Lothiraldancommented, Dec 2, 2015

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:

class JobRunForm extends React.Component {

    shouldComponentUpdate = (nextProps, nextState, nextContext) => {
        return !(nextProps == this.props) ||
          !(nextState == this.state) ||
          !(nextContext == this.context);
    }

    render() {
      return (
        <form className="form-horizontal style-form">
              <Select
                  name="form-matcher-name"
                  options={this.props.matchers}
                  onChange={this.props.onChange}
                  searchable={true}
                  placeholder={'Glob'}
              />
       </form>
     )

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found