react-select: Passing value `undefined` does not clear internal state
I’m working on a project where we use a react-select Select component together with 2 radio inputs (see screenshot below). When the other radio is selected we set the value for the select in our local state to undefined using the onChange handler. So we pass the properties value={undefined} and no defaultValue. We also set isClearable={false}, isSearchable={false} and if the value is undefined isDisabled={true}. As you can see the value of the Select component is not cleared and set back to the placeholder.

When inspecting the components react-select creates with the React devtools I noticed the props and state of the StateManager look like this:

It seems that, even though the value is undefined and the defaultValue is null, the state is not cleared. which is odd as the stateManager.js file includes the following lines:
state = {
inputValue:
this.props.inputValue !== undefined
? this.props.inputValue
: this.props.defaultInputValue,
menuIsOpen:
this.props.menuIsOpen !== undefined
? this.props.menuIsOpen
: this.props.defaultMenuIsOpen,
value:
this.props.value !== undefined
? this.props.value
: this.props.defaultValue,
};
So this.state.value should be null. And then the following code is used to get that value and pass it to the Select component:
getProp(key: string) {
return this.props[key] !== undefined ? this.props[key] : this.state[key];
}
so it should still be null as far as I can tell.
I cannot really explain why this happens but figured I’d make an issue as it feels like a bug. If it’s not and I forgot to pass some prop correctly I’d also be happy to hear it 😃
Ultimately we solved this for our use case by adding a key prop to our Select component so that it remounts when the value changes which does clear the value and sets it back to the placeholder. This works but it is not the nicest fix of course.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 42
- Comments: 18
Commits related to this issue
- Fixed bug where the label for the Single Value Select did not change if the value of the formik field was set to null Basically, the formik value of a select field got changed to undefined or null, b... — committed to stiftungswo/better-dime by andyundso 6 years ago
- Fixed bug where the label for the Single Value Select did not change if the value of the formik field was set to null Basically, the formik value of a select field got changed to undefined or null, b... — committed to stiftungswo/better-dime by andyundso 6 years ago
The API docs say the type for
valueisThat agrees with
ValueTypein the code:I would expect
nullorundefinedto clear the selection, but it does not.In normal React way, having the
valueprop with a value ofundefinedon a form element indicates it being uncontrolled, meaning it does not rely on a prop for its value. React would give you an error in the console if you change the value of thevalueprop on a form element toundefined(switching from controlled to uncontrolled).react-selectinstead gracefully switches to being uncontrolled and manages the value by itself.In short, using
undefinedto clear the value is in general bad practice. Usenullinstead when you clear the value of anySelectcomponent provided by this library.Update: In recently updated docs, it says in a piece about upgrading from old key/label props to use
value={options.filter(({value}) => value === this.state.value)}instead when using simple values, and I’ve found that it works infinitely better thanvalue={options.find(({value}) => value === this.state.value) || ''}Got this same issue today, can verify that using
nullinstead ofundefineddid the trick.When using Select as uncontrolled component the above hack with the
clearValue()did not work for me. What worked was setting thevalueasnull.You can see the comparison here: https://codesandbox.io/s/epic-matsumoto-3p6j2
I’ve also been running into this. Prior to v2 we mapped all values to the value key, but with the 2.0 changes that only allow an object for the value key, many of our selects changed:
Given these example options:
we went from
to
since the value prop is required to be a full object in v2, instead of just a string as we were setting before.
The only way I’ve found to work around it is by setting
value={options.find(o => o.value === this.state.value) || '')but it’s definitely a bit duct-tapey.Though our upgrade solution is a bit non-standard, I would still think that the value being changed to undefined should get reflected in the Select’s internal state, instead of holding onto the last value.
If you get a ref to the StateManager, you can do it like this:
Then make the call when you want to clear it.
Also running into this and can confirm using
nullinstead ofundefinedworks. As a workaround I’m just convertingnulltoundefinedin my HOC wrapper’sonChange.Or, using the above example, once you have the ref assigned to the component you can execute this code to clear/reset the select:
this.selectRef.select.clearValue();As pointed out by @Rall3n, an undefined value is not interchangeable with null.
Due to the inactivity of this issue and apparent discovery/resolution, we will be closing this. If there are any other followups regarding this confusion, please feel free to open a Discussion in the discussions section.
Thanks @douweknook for the
keyworkaround