javivelasco / react-tunnels Goto Github PK
View Code? Open in Web Editor NEW๐ Render React components in placeholders that are placed somewhere else in the component tree.
License: MIT License
๐ Render React components in placeholders that are placed somewhere else in the component tree.
License: MIT License
Hi!
I've been testing the library for SSR and it seems that is not working, in case you could check that.
Looks like the implementation of TunnelPlaceholder
is treating render props as if they were full fledged components by using React.creatElement
.
While this is cool because it let you expose a double API:
<TunnelPlaceholder>
{props => <MyComponent {...props} />}
</TunnelPlaceholder>
// is equal to
<TunnelPlaceholder>
{MyComponent}
</TunnelPlaceholder>
But this has a major caveat: when using the render-prop API every re-render will unmount and remount the whole placeholder children!
This is because between different render phases React thinks this is a completely new component and that it cannot reconciliate in any way.
What you actually want to achieve (probably) is something along the lines of what react-router
does by exposing three different props:
render
which is a render prop,children
which is a render prop,component
which is treated as a ComponentType or a Functional Stateless Component.And you can have a look at how they implement their #render
method too.
Iโll make a PR soon so that if you are ok with the approach you can accept it right away.
The readme example imports from preact-slots
, was that intentional?
There is no preact dependency on package.json so I assume it was not.
So I have the code below.
SomeComponentThatRendersToTunnel will try to put something inside footer-tunnel but it wont show on first render. My guess is that it's because it's below what will try to put something inside it. If I move the tunnel above the component it does render.
Is this a bug or expected? Is there a way to get around it?
<TunnelProvider>
<>
<SomeComponentThatRendersToTunnel />
<div className={styles.flexFooter}>
<TunnelPlaceholder id="footer-tunnel" />
</div>
</>
</TunnelProvider>
Currently the tunneled component reads the context of where it is rendered at. It would be useful to redirect the context the same way it is done with props.
If you don't have time to do it, do you think it would be possible?
What about using react portals to render children into DOM nodes rendered and registered by other react components previously? React portals do redirect context. Do you see any potential problem with that?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.