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.

Passing value `undefined` does not clear internal state

See original GitHub issue

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.

schermafbeelding 2018-09-25 om 12 14 33

When inspecting the components react-select creates with the React devtools I noticed the props and state of the StateManager look like this: schermafbeelding 2018-09-25 om 12 16 28

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:40
  • Comments:18

github_iconTop GitHub Comments

23reactions
jamesarosencommented, Mar 14, 2019

The API docs say the type for value is

One of <
  Object,
  Array<Object>,
  null,
  undefined
>

That agrees with ValueType in the code:

export type ValueType = OptionType | OptionsType | null | void;

I would expect null or undefined to clear the selection, but it does not.

7reactions
yagudaevcommented, Jun 3, 2020

Got this same issue today, can verify that using null instead of undefined did the trick.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix undefined state passed as props, into child ...
When passing the state from the parent to the child via props, you don't need to set a state in the child. In...
Read more >
Uncontrolled Components - React
To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form...
Read more >
Function.prototype.bind() - JavaScript - MDN Web Docs - Mozilla
The bound function will store the parameters passed — which include the value of this and the first few arguments — as its...
Read more >
Watch Out for Undefined State - Dave Ceddia
The Fix(es) ... This is easy to fix. The simplest way: initialize state with reasonable default values in the constructor. ... This is...
Read more >
Components - React Select
These components are given all the current props and state letting you achieve ... is undefined; When the select is disabled; When the...
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