Fix crash when using DisclosureButton
inside of a DisclosurePanel
when the Disclosure
is open by default
#3465
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where React hooks were called unconditionally>
The
PopoverButton
andDisclosureButton
act as aCloseButton
when used inside of a panel. We conditionally handled theref
when it's inside a panel. To ensure that the callback is stable, the conditionally used function was wrapped in auseEvent(…)
hook.This seemed to be ok (even though we break the rules of hooks) because a button can only be in a panel or not be in a panel, but it can't switch during the lifetime of the button. Aka, the rules of hooks are not broken because all code paths lead to the same hooks being called.
But... it can be called conditionally, because the way we know whether we are in a panel relies on a state value which comes from context and is populated by a
useEffect(…)
hook.The reason we didn't catch this in the
Disclosure
component, is because all the state is stable and known by the time theDisclosurePanel
opens. But if you use thedefaultOpen
prop, theDisclosurePanel
is already open and then the state is not ready yet (because we have to wait for theuseEffect(…)
hook).Long story short, moved the
isWithinPanel
check inside theuseEvent(…)
hook that holds the stable function which means that we don't call this hook unconditionally anymore.