javivelasco / react-tunnels

🚇 Render React components in placeholders that are placed somewhere else in the component tree.
MIT License
398 stars 11 forks source link

Suboptimal approach to render props #8

Open yuchi opened 6 years ago

yuchi commented 6 years ago

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:

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.

yuchi commented 6 years ago

Whooops! There’s a PR exactly for this! See #6