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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
SelectorDropdowncomponents, and it seems like I should useSelectbecause the dropdown does not contain styling or need multi-select.I'm running into trouble with the
onChangefor the Select component and forcing rerenders when the value changes.Here's the implementation of the component (slightly generalized):
Each of the
sizeOptionswithin theSelectis 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, causingselectedItemto change (I can tell thatselectedItemis changing as it should be through log statements). However, when the parent component rerenders, theSelectis not rerendering and does not reflect the updated value ofselectedItem. The only value theSelectdropdown 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 associatedonClicksare called as expected and the value ofselectedItemchanges, even though theSelectcomponent doesn't reflect this change.Is this a supported use for
Selectas a controlled component, or should I focus my efforts on trying to makeDropdownwork for this use case instead?Beta Was this translation helpful? Give feedback.
All reactions