Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the select component in controlled mode? #22

Open
vtrphan opened this issue Jun 29, 2022 · 1 comment
Open

How to use the select component in controlled mode? #22

vtrphan opened this issue Jun 29, 2022 · 1 comment

Comments

@vtrphan
Copy link

vtrphan commented Jun 29, 2022

I don't see a value prop for the select component. How to use it in a controlled mode with React state?

@Orlando-Silva
Copy link

Orlando-Silva commented Jul 25, 2022

For the past hours I've been trying to figure it out by looking at the source code and it seems that the property name it's equivalent to Select's value property. In other words, if you use name as your value prop you will be able to use your component in a controlled manner.

Here's how I've managed it.

const options = [
    { "title": "China", "value": 52 },
    { "title": "South Africa", "value": 86 },
    { "title": "Italy", "value": 26 }
]

const [selectValue, setSelectValue] = useState("")

<Insect
  type="select"
  options={options}
  onSelect={(value) => setSelectValue(value) }
  name={selectValue}
/>

I've also highly recommend you to use the defaultOption property as it seems that if you just run the code above, when you first set a value, the setter doesn't properly register it but on a second try (and on subsequent tries) it properly does so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants