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.
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
fix(modal): onOpenChange triggering when modal opens #3902
base: canary
Are you sure you want to change the base?
fix(modal): onOpenChange triggering when modal opens #3902
Changes from all commits
4741a84
e4749c6
dbf2c49
d20d2f2
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 came across this interesting resource on the React docs (https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes) that suggests alternative approaches to
useEffect
in some scenarios. It might be worth considering if it applies to this particular use case.In the example provided, they achieve a similar outcome by storing information from previous renders. I took a stab at adapting it here (untested):
What do you think?
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.
Thanks, It seems a very nice idea to avoid useEffect. I'll test and see if it works.
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 tested, actually the onOpenChange is eventually changing the isOpen state. And we shouldn't directly update parent state inside child without useEffect.
I tried though and got the warning : "Cannot update a component (
App
) while rendering a different component (NextUI.Modal
)".Let me know if I am missing something.
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.
Since
isOpen
is managed withinuseDisclosure
whether it is controlled or uncontrolled, is it necessary to pass it as an argument?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.
Yes, in this case its needed. Because when the
onClose
is called its modifiying the internal state as I said earlier (isOpen
becomesfalse
), but at this point theisOpen
which is inuseDisclosure
is stilltrue
.Initially the definition of
onOpenChange
was like thisisOpen ? onClose : onOpen
, thats why it was working whenonClose
used to get called.But now that will cause infinite render due to
useEffect
, thats why I reversed it and and added and argument.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.
Isn't that the expected behavior?
state.close
(internal state becomesfalse
).onOpenChange
is called (at this point,useDisclosure
state is stilltrue
).isOpen
istrue
,onClose
gets called.useDisclosure
state becomesfalse
(syncing the internal state with useDisclosure).Accepting an argument would be a breaking change, so it might be better to leave it as is for backward compatibility.
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.
Could you clarify in what cases the
useEffect
bug occurs?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.
You press the open button → external state becomes true. That will remount modal Component.
useOverlayTriggerState
will re-run with isOpentrue
. After this,useEffect
will be called and it will call the outer onOpenChange(which was received as prop to modal), without passing argument((useDisclosure
state istrue
here).Since
isOpen
istrue
,onClose
gets called(at this point,useDisclosure
state becomesfalse
, but internal state is still true). (Not sync)Now, you press the close button →
state.close
(internal state becomesfalse
).onOpenChange
is called (at this point,useDisclosure
state isfalse
), which meansonOpen
gets called but hereonClose
needs to get called.Ya, i thought that it might be a breaking change, but we are also passing state while calling onOpenChange from
useOverlayTriggerState
nextui/packages/components/modal/src/use-modal.ts
Line 124 in 8a33eab
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.
Oh, so the
useEffect
added inuseModal
is the issue. Hmm, with the current implementation, it might introduce new bugs. Could you consider exploring alternative implementation methods?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.
What do you think? @jrgarciadev