JedWatson / JedWatson/react-select

Cannot clear the value to induce defaultValue

Open
#4,942 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

defaultValue is used if initial value is undefined but once value is set to anything then new value becomes the default. I.e. subsequent undefined value will show the value that was previously set.

https://codesandbox.io/s/codesandboxer-example-forked-2j6n7?file=/example.tsx

import React, { Fragment, useState } from "react";
import Select from "react-select";

export default function Example() {
  const options = [
    { label: "zero", value: 0 },
    { label: "one", value: 1 },
    { label: "two", value: 2 }
  ];
  const [value, setValue] = useState(undefined);
  return (
    <Fragment>
      <Select
        defaultValue={options[0]}
        value={value}
        options={options}
        placeholder={"some placeholder"}
        onChange={setValue}
      />
      <button onClick={() => setValue(null)}>Set to null</button>
      <button onClick={() => setValue(undefined)}>Set to undefined</button>
      <button onClick={() => setValue([] as any)}>Set to []</button>
    </Fragment>
  );
}

This example starts with undefined value and correctly shows default of zero.
Change value to "one" then click "Set to undefined" and you will still see "one".
Click "Set to null" and you will get placeholder but click "Set to undefined" and it will be "one" again.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox example and trace how Select handles defaultValue alongside controlled value updates. Reproduce the transitions from an option to undefined, null, and an empty array; done means setting undefined no longer restores the previously selected option while the other demonstrated behaviors remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.