JedWatson / JedWatson/react-select
Cannot clear the value to induce defaultValue
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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