React-select: [v2] Setting value to undefined doesn't show the placeholder text

Created on 14 Aug 2018  路  4Comments  路  Source: JedWatson/react-select

Setting the value of the select component to null/undefined does not reset the control to a state where the placeholder text is displayed:

https://codesandbox.io/s/jnkzp95r4w

Most helpful comment

Hi, @denchp setting the value to null should clear the value internally.
https://codesandbox.io/s/98n2v97plw

The reason this is happening is because the negative result of Array.prototype.find() is undefined and not null.

react-select takes an undefined value prop as indication for it to use internal state to manage the selected value. To clear the selected value in the component please explicitly set the value prop to null.

This is very much inline with the intention of these two values in javascript, where undefined is an unset value and null is explicitly the intentional absence of an object value.

issue closed

All 4 comments

try to set the value to zero
see https://codesandbox.io/s/9yoxy46l0p

Hi, @denchp setting the value to null should clear the value internally.
https://codesandbox.io/s/98n2v97plw

The reason this is happening is because the negative result of Array.prototype.find() is undefined and not null.

react-select takes an undefined value prop as indication for it to use internal state to manage the selected value. To clear the selected value in the component please explicitly set the value prop to null.

This is very much inline with the intention of these two values in javascript, where undefined is an unset value and null is explicitly the intentional absence of an object value.

issue closed

Thanks @gwyneplaine. Replacing { value: '', label: '' } with null fixed my placeholder issue.

Seems kind of silly that if I backspace away the entire inputValue and pass value={undefined}, the placeholder doesn't appear.

To make it work properly I've had to use:

  <CreatableSelect
    value={value === undefined ? null : value}
  />

@gwyneplaine I feel like this is a (quite minor) bug and shouldn't be closed. The issue remains in 3.0.8. I didn't realize 3.1.0 was out, so I'll give that a shot and report back.

Edit: Issue persists in 3.1.0. Perhaps there's a good reason for requiring null to clear the rendered text in which case this isn't a bug? Right now I believe it's a bug though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mbonaci picture mbonaci  路  3Comments

ericj17 picture ericj17  路  3Comments

pashap picture pashap  路  3Comments

pgoldweic picture pgoldweic  路  3Comments

geraldfullam picture geraldfullam  路  3Comments