-
Notifications
You must be signed in to change notification settings - Fork 7
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
no-issue: Set the initial selection of a multi-value autocomplete input #5326
Conversation
- This makes it easier for users to add/remove from the existing selection rather than needing to remember it
@@ -37,12 +39,27 @@ const ReduxAutocompleteComponent = React.memo( | |||
placeholder, | |||
helperText, | |||
}) => { | |||
const [hasSetInitialSelection, setHasSetInitialSelection] = useState(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a better way to ensure this only is done once, but this is what first came to mind...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is soooo nice to have fixed, thank you!
React.useEffect(() => { | ||
return () => { | ||
onClearState(); | ||
}; | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume this is intentional, but what makes this different from:
React.useEffect(onClearState, []);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you return a function from useEffect()
that function will get run whenever the component is removed from the DOM (it's called the cleanup function). So in this case it will clear the autocomplete when it's removed from the DOM. Whereas useEffect(onClearState, [])
would clear the autocomplete when it is first rendered. Possibly that might result with the same behaviour in practice, but clearing in the cleanup function feels a bit more elegant.
We should probably have a comment explaining this though, I didn't know what it was doing when I first saw it either.
Issue https://beyondessential.slack.com/archives/C011G5L1J12/p1704949776764939:
This makes it easier for users to add/remove from the existing selection rather than needing to remember it