Support for reparenting
See original GitHub issueWhen writing a component that contains a set of large subtrees that stay relatively the same, but are simply moved around such that React’s virtual DOM diffing can’t detect the movement, React will end up recreating huge trees it should simply be moving.
For example, pretend blockA
and blockB
are very large structures. They may be made of several levels of children and components. For example one could be the entire page contents and the other the sidebar, while this render()
is the page root.
render() {
var blockA = <div>AAA</div>,
blockB = <div>BBB</div>;
if ( this.props.layoutA ) {
return <div>
<div className="something">{blockB}</div>
<div className="something">{blockA}</div>
</div>;
} else {
return <div>
{blockA}
{blockB}
</div>;
}
}
Because the blocks aren’t at the same level React cannot see the relation between these blocks and key
cannot be used to give React any hints. As a result, when layoutA
is changed, instead of the two blocks being moved to their new location the entire page is essentially completely unrendered and then re-rendered from scratch.
I understand why this is the case. It would be far to expensive for React to be able to detect movement of nodes like this.
But I do believe we need a pattern to hint to React that this component has large blocks that may be moved around at different levels.
Note that there may be a component in between the rendering component root and the block. So parent semantics scoped to the nearest component won’t work. This’ll need owner scoping.
I understand that React is trying to eliminate the need for React.createElement to be used and owner scoping within special attributes interferes with that. So instead of a component scoped key=""
variant I think a method/object style interface kind of like React.addons.createFragment
might work.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:68
- Comments:39 (13 by maintainers)
I’ve opened an RFC with a proposal for an API that allows for reparenting:
https://github.com/reactjs/rfcs/pull/34
It turns out you can solve reparenting pretty neatly with portals.
I’ve built a library to do that, based on some of the discussion here and a couple of other issues (#13044, #12247). It lets you define some content in one place, and render & mount it there once, then place it elsewhere and move it later, all without remounting or rerendering.
I’m calling it reverse portals, since it’s the opposite model to normal portals: instead of pushing content from a React component to distant DOM, you define content in one place, then declaratively pull that DOM into your React tree elsewhere. I’m using it in production to reparent expensive-to-initialize components, it’s been working very nicely for me!
Super tiny, zero dependencies: https://github.com/httptoolkit/react-reverse-portal. Let me know if it works for you 👍