-
-
Notifications
You must be signed in to change notification settings - Fork 226
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: prevent from creating multi portals #292
Conversation
@@ -858,10 +860,13 @@ export function generateTrigger( | |||
let portal: React.ReactElement; | |||
// prevent unmounting after it's rendered | |||
if (popupVisible || this.popupRef.current || forceRender) { | |||
if (!this.portalContainer) { | |||
this.portalContainer = this.getContainer(); |
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.
Will this also execute multiple time as FC first render?
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.
render
will be double-invoked, but if (!this.portalContainer)
can ensure getContainer
is called only once.
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.
So class component will be the same instance but FC will not. Right?
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.
So are you going to rewrite Portal
to a class component? 😂
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.
Noop. Just confirm of this : )
Pls patch a comment here to tell that refactor will FC will break the behavior which helps refactor in future.
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.
Updated, maybe not fully understood.
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 mean comment this line: https://github.com/react-component/trigger/pull/292/files#diff-0b5adbfe7b36e4ae2f479291e20152e33e940f7f265162d77f40f6bdb5da7405R865
For example:
In React.StrictMode component will call render multiple time in first mount. When you want to refactor with FC,
useRef
will also init multiple time and point to differentuseRef
instance which will create multiple element (This multiple render will not trigger effect so you can not clean up this in effect). But this is safe with class component since it always point to same class instance.
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.
Okay, nice clear
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.
Actually, it's the same as FC in some ways.
class component behavior:
constructor r3mjrhp00qn
constructor 7df2h5d7k9e
render 7df2h5d7k9e
render 7df2h5d7k9e
just because that we call getContainer
in render
(which happened in second one, 7df2h5d7k9e).
Codecov Report
@@ Coverage Diff @@
## master #292 +/- ##
==========================================
+ Coverage 89.36% 89.42% +0.05%
==========================================
Files 11 11
Lines 536 539 +3
Branches 137 138 +1
==========================================
+ Hits 479 482 +3
Misses 57 57
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
Strange. antd CI failed. https://github.com/ant-design/ant-design/runs/5984746938?check_suite_focus=true |
I'll take a look. |
Let me revert this first. |
This patch is compatible, and should not cause any breaking change. Curiously, |
Find the reason. Origin getContainer is called in Portal render. But current logic makes render div on the Trigger render which raise the render before. Let me patch this. |
Got it, it should like this:
|
close react-component/util#293