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.

Rerendering component on onChange causes default value to appear

See original GitHub issue

If I have the following select component:

<Select onChange={this.handleChange}
    ref="inputName"
    name="inputName"
    options={inputOptions}
    value="Please select one of the following:"/>

And I define the following onChange event:

handleChange: function (value) {
    this._someStateValue = value === 'new';
    this.forceUpdate();
},

or, for that matter, this:

handleChange: function (value) {
    var someStateValue = value === 'new';
    this.setState({_someStateValue: someStateValue});
},

Note that both of these approaches causes a component re-render.

Then, whenever I change the Select, triggering the onChange event, which in turn causes the component to re-render, the default value is displayed and not the value in the select options that I have just selected. Subsequently re-selecting the desired select option will then display it, most likely because react does a diff check on the state of the component and does not trigger a re-render (my best guess).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

1reaction
andrewhlcommented, Oct 13, 2015

I found a workaround: making the value of the Select a function that checks the state of the component, and the onChange another function that updates the state to whatever value is passed along. I don’t love this, but it seems to be working. Mind you, I’ve only tested with a limited use case; I can’t yet confirm that it resolved the original issue I posted. I will test against my issue when I get a chance.

<Select onChange={this.updateValue} value={this.getValue()} />

...

updateValue: function(value) {
    this.setState({value: value});
},
getValue: function() {
    if (!this.state.value) {
        return 'Some default text';
    }
    return this.state.value;
}
0reactions
jossmaccommented, Mar 17, 2020

Version 1 of react-select is no longer supported.

In the best interest of the community we’ve decided to spend the time we have available on the latest version.

We apologise for any inconvenience.

Please see:

  1. v1 to v2 upgrade guide
  2. v1 documentation and examples
Read more comments on GitHub >

github_iconTop Results From Across the Web

Re-render DefaultValue when Value Changes in React
defaultValue is supposed to allow an input to receive some starting data but then React usually will forget that it was ever set....
Read more >
defaultValue change does not re-render input - Stack Overflow
I found what seems to be a pretty good solution to this: Use the key prop to force rendering of an entirely new...
Read more >
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >
FAQs | React Hook Form - Simple React forms validation
Double check if you are using value instead of defaultValue . React Hook Form is focusing on uncontrolled inputs, which means you don't...
Read more >
Uncontrolled Components - React
In the React rendering lifecycle, the value attribute on form elements will override the value in the DOM. With an uncontrolled component, you...
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