Rerendering component on onChange causes default value to appear
See original GitHub issueIf 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:
- Created 8 years ago
- Reactions:2
- Comments:10
Top 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 >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 found a workaround: making the
value
of theSelect
a function that checks the state of the component, and theonChange
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.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: