Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid double-rendering InPortal child initially if props aren't overridden #19

Closed
wants to merge 1 commit into from

Conversation

bduffany
Copy link

@bduffany bduffany commented Dec 13, 2020

Bug: The repo's description "build an element once, move it anywhere" is technically incorrect; really it's "build an element twice, move it anywhere." This PR fixes that problem 馃槂

Before: https://codesandbox.io/s/affectionate-bouman-y201q?file=/src/index.tsx

After: https://codesandbox.io/s/bold-hoover-il7we?file=/src/react-reverse-portal.tsx

@CLAassistant
Copy link

CLAassistant commented Dec 13, 2020

CLA assistant check
All committers have signed the CLA.

@bduffany bduffany changed the title Don't clone element if props aren't overridden Avoid double-rendering an element if props aren't overridden Dec 13, 2020
@bduffany bduffany changed the title Avoid double-rendering an element if props aren't overridden Avoid double-rendering InPortal child if props aren't overridden Dec 13, 2020
@bduffany bduffany changed the title Avoid double-rendering InPortal child if props aren't overridden Avoid double-rendering InPortal child initially if props aren't overridden Dec 13, 2020
@pimterry
Copy link
Member

pimterry commented Jan 5, 2021

I was surprised that this works, because nodeProps should never be falsey. It's initialized as {} (in getInitialPortalProps) and setPortalProps later on is only ever called with object values too.

I took a closer look at your example: the code there doesn't match this PR. Instead of !this.state.nodeProps, it uses this.state.nodeProps and so it never clones the child, which is why it works in the example case (but this disables using all props from OutPortals).

That said, the goal here 100% makes sense to me, and I'd love to avoid that unnecessary extra render if possible. How about doing this with something like Object.keys(nodeProps).length === 0 instead? I think that's what you're looking for.

It'd be helpful if you could copy your example in as a new storybook case too, so we can play with this within the project itself, against the committed code, rather than needing the sandboxes.

@pimterry
Copy link
Member

pimterry commented Jun 8, 2021

Happy to continue this, but there's some changes required as above. I'll close it for now, but feel free to ping here if you're still interested in working on this.

@pimterry pimterry closed this Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants