question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for reparenting

See original GitHub issue

When 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:open
  • Created 8 years ago
  • Reactions:68
  • Comments:39 (13 by maintainers)

github_iconTop GitHub Comments

18reactions
dantmancommented, Mar 12, 2018

I’ve opened an RFC with a proposal for an API that allows for reparenting:

https://github.com/reactjs/rfcs/pull/34

16reactions
pimterrycommented, Aug 26, 2019

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 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reparenting in Therapy - Verywell Mind
Reparenting is a therapeutic technique that helps clients unlearn unhealthy patterns of communication and behaviors acquired in childhood.
Read more >
Reparenting to Heal the Wounded Inner Child
Reparenting yourself allows you to give yourself all the love, respect, and dignity you did not receive in childhood.
Read more >
How to Reparent Yourself: A Step-by-Step Guide
This 7-step guide covers what reparenting is, why it's important, and the process of how to reparent yourself to heal from childhood abuse....
Read more >
Reparenting in Therapy: Why You Should Consider It
“Reparenting helps an individual repair attachments and develop more secure and healthy relationships,” said Rachel O'Neill, an Ohio-licensed ...
Read more >
What is Reparenting, And is it Helpful?
In its simplest form, reparenting is the act of learning how to give yourself the support you may not have received as a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found