Using Select as a controlled component #32330
Unanswered
angelusmcnally
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm migrating a command bar item to a dropdown that triggers a function when the user selects a different option. I know I can create this with either the
Select
orDropdown
components, and it seems like I should useSelect
because the dropdown does not contain styling or need multi-select.I'm running into trouble with the
onChange
for the Select component and forcing rerenders when the value changes.Here's the implementation of the component (slightly generalized):
Each of the
sizeOptions
within theSelect
is an<option>
that's wrapped around each of thecommandBarItems
. Each has an associatedonClick
, which bubbles the change elsewhere and ultimately rerenders the parent of this component, causingselectedItem
to change (I can tell thatselectedItem
is changing as it should be through log statements). However, when the parent component rerenders, theSelect
is not rerendering and does not reflect the updated value ofselectedItem
. The only value theSelect
dropdown will display is the first element ofsizeOptions
(which is the same as the first element ofcommandBarItems
. When I select other options in the dropdown, the associatedonClicks
are called as expected and the value ofselectedItem
changes, even though theSelect
component doesn't reflect this change.Is this a supported use for
Select
as a controlled component, or should I focus my efforts on trying to makeDropdown
work for this use case instead?Beta Was this translation helpful? Give feedback.
All reactions