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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ref to Fragment #32421

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add ref to Fragment #32421

wants to merge 4 commits into from

Conversation

jackpope
Copy link
Contributor

This API is experimental and subject to change or removal.

Fragment Refs

This PR extends React's Fragment component to accept a ref prop. The Fragment's ref will attach to a custom host instance, which will provide an Element-like API for working with the Fragment's host parent and host children.

Here I've implemented addEventListener and removeEventListener to get started but we'll be iterating on this by adding additional APIs in future PRs. This sets up the mechanism to attach refs and track children. The FragmentInstance is implemented in react-dom here but is planned for Fabric as well.

The API works by targeting the first level of host children and proxying Element-like APIs to allow developers to manage groups of elements or elements that cannot be easily accessed such as from a third-party library or deep in a tree of Functional Component wrappers.

import {Fragment, useRef} from 'react';

const fragmentRef = useRef(null);

<Fragment ref={fragmentRef}>
  <div id="A" />
  <Wrapper>
    <div id="B">
      <div id="C" />
    </div>
  </Wrapper>
  <div id="D" />
</Fragment>

In this case, calling fragmentRef.current.addEventListener() would apply an event listener to A, B, and D. C is skipped because it is nested under the first level of Host Component. If another Host Component was appended as a sibling to A, B, or D, the event listener would be applied to that element as well and any other APIs would also affect the newly added child.

This is an implementation of the basic feature to collect early feedback. There are still areas to explore regarding the timing of tracking/untracking children, adding listeners, and interactions with effects. As well as additional feature work on the FragmentInstance API methods.

@react-sizebot
Copy link

react-sizebot commented Feb 18, 2025

Comparing: a84862d...9034301

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.02% 515.71 kB 515.80 kB = 92.09 kB 92.09 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 562.25 kB 562.25 kB +0.03% 100.08 kB 100.11 kB
facebook-www/ReactDOM-prod.classic.js +0.92% 636.70 kB 642.54 kB +0.90% 112.08 kB 113.09 kB
facebook-www/ReactDOM-prod.modern.js +0.93% 627.02 kB 632.86 kB +0.91% 110.50 kB 111.51 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.69 kB 113.91 kB = 26.32 kB 22.63 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.60 kB 113.82 kB = 26.27 kB 22.58 kB
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.53 kB 113.73 kB = 26.30 kB 22.60 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.53 kB 113.73 kB = 26.30 kB 22.60 kB
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.44 kB 113.65 kB = 26.25 kB 22.56 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.44 kB 113.65 kB = 26.25 kB 22.56 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-www/ReactDOM-prod.modern.js +0.93% 627.02 kB 632.86 kB +0.91% 110.50 kB 111.51 kB
facebook-www/ReactDOM-prod.classic.js +0.92% 636.70 kB 642.54 kB +0.90% 112.08 kB 113.09 kB
facebook-www/ReactDOMTesting-prod.modern.js +0.91% 641.74 kB 647.58 kB +0.87% 114.21 kB 115.20 kB
facebook-www/ReactDOMTesting-prod.classic.js +0.90% 651.42 kB 657.26 kB +0.86% 115.79 kB 116.79 kB
facebook-www/ReactDOM-profiling.modern.js +0.89% 654.41 kB 660.25 kB +0.86% 114.44 kB 115.42 kB
facebook-www/ReactDOM-profiling.classic.js +0.88% 664.14 kB 669.98 kB +0.85% 116.03 kB 117.01 kB
facebook-www/ReactReconciler-prod.modern.js +0.71% 485.32 kB 488.76 kB +0.62% 77.67 kB 78.15 kB
facebook-www/ReactReconciler-prod.classic.js +0.69% 495.59 kB 499.03 kB +0.59% 79.28 kB 79.74 kB
facebook-www/ReactDOM-dev.modern.js +0.64% 1,108.83 kB 1,115.90 kB +0.62% 185.15 kB 186.31 kB
facebook-www/ReactDOM-dev.classic.js +0.63% 1,117.97 kB 1,125.04 kB +0.62% 186.90 kB 188.05 kB
facebook-www/ReactDOMTesting-dev.modern.js +0.63% 1,125.73 kB 1,132.80 kB +0.60% 189.03 kB 190.17 kB
facebook-www/ReactDOMTesting-dev.classic.js +0.62% 1,134.88 kB 1,141.95 kB +0.60% 190.74 kB 191.89 kB
facebook-www/ReactART-prod.modern.js +0.57% 373.47 kB 375.61 kB +0.55% 62.86 kB 63.21 kB
facebook-www/ReactART-prod.classic.js +0.56% 383.42 kB 385.55 kB +0.55% 64.46 kB 64.82 kB
facebook-www/ReactReconciler-dev.modern.js +0.53% 762.53 kB 766.56 kB +0.31% 119.84 kB 120.21 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer.production.js +0.53% 36.48 kB 36.68 kB +0.31% 6.82 kB 6.85 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer.production.js +0.53% 36.51 kB 36.70 kB +0.31% 6.85 kB 6.88 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer.production.js +0.53% 36.51 kB 36.71 kB +0.31% 6.86 kB 6.88 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.52% 36.61 kB 36.80 kB +0.31% 6.84 kB 6.86 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.52% 36.64 kB 36.83 kB +0.31% 6.87 kB 6.89 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.52% 36.64 kB 36.83 kB +0.29% 6.88 kB 6.90 kB
facebook-www/ReactReconciler-dev.classic.js +0.52% 771.74 kB 775.76 kB +0.31% 121.51 kB 121.89 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer.development.js +0.50% 40.68 kB 40.88 kB +0.30% 7.42 kB 7.44 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer.development.js +0.50% 40.70 kB 40.90 kB +0.31% 7.45 kB 7.47 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer.development.js +0.50% 40.71 kB 40.91 kB +0.31% 7.45 kB 7.47 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.49% 40.82 kB 41.02 kB +0.30% 7.43 kB 7.46 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.49% 40.84 kB 41.04 kB +0.29% 7.46 kB 7.49 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.49% 40.85 kB 41.05 kB +0.31% 7.47 kB 7.49 kB
facebook-www/ReactART-dev.modern.js +0.40% 658.04 kB 660.69 kB +0.31% 104.39 kB 104.71 kB
facebook-www/ReactART-dev.classic.js +0.40% 667.54 kB 670.19 kB +0.32% 106.24 kB 106.57 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.69 kB 113.91 kB = 26.32 kB 22.63 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.60 kB 113.82 kB = 26.27 kB 22.58 kB
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.53 kB 113.73 kB = 26.30 kB 22.60 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js = 134.53 kB 113.73 kB = 26.30 kB 22.60 kB
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.44 kB 113.65 kB = 26.25 kB 22.56 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.js = 134.44 kB 113.65 kB = 26.25 kB 22.56 kB

Generated by 🚫 dangerJS against f52d849

_eventListeners: Array<StoredEventListener>,
parentInstance: Instance | Container,
appendChild(child: Element): void,
removeChild(child: Element): void,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appendChild/removeChild doesn't make much sense as publicly accessible APIs. A fragment doesn't even have a single parent and the children could be spread out.

It's also not something you should mess with in user space since it's an internal set which fragile semantics. E.g. this implementation doesn't preserve any kind of ordering guarantees.

There's no need for the internal methods to operate on these methods so they don't need to exist. Just have appendChildToFragmentInstance add to the _children Set directly.

@@ -214,6 +218,25 @@ function getHostParentFiber(fiber: Fiber): Fiber {
);
}

export function getFragmentInstanceParent(
fiber: Fiber,
): null | FragmentInstance {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that there can be more than one parent Fragment. When I wrap a Component I shouldn't have to know whether or not there's another Fragment wrapping it. It seems like it's set up now, adding a Fragment would block parents from inspecting the insides - even by accident.

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's kind of two approaches to this. One is to keep track of any mounted instances during the commit phase using an effect. That's basically what the current approach does. However, that has some perf considerations of doing that managing continuously as children get added or removed. Especially on a large set. It's also difficult to preserve document order which means that the current implementation basically has no consistent order. As things get added or removed, things end up basically random.

Another approach would be to keep track of the currently mounted Fiber and then traverse that Fiber when an operation is done. That way we only pay extra cost when you actually do one of the operations but each operation is slightly slower. A benefit of this is that document order is easy to preserve since the traversal is always in document order.

Document order can become important for things like focusing the first focusable element. Otherwise that can end up being random.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants