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

Implement HostSingleton #25426

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes';
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoTestSelectors';
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMicrotasks';
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoResources';
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoSingletons';

export function appendInitialChild(parentInstance, child) {
if (typeof child === 'string') {
Expand Down
10 changes: 8 additions & 2 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {
enableTrustedTypesIntegration,
enableCustomElementPropertySupport,
enableClientRenderFallbackOnTextMismatch,
enableHostSingletons,
} from 'shared/ReactFeatureFlags';
import {
mediaEventTypes,
Expand Down Expand Up @@ -312,12 +313,17 @@ function setInitialDOMProperties(
// textContent on a <textarea> will cause the placeholder to not
// show within the <textarea> until it has been focused and blurred again.
// https://github.com/facebook/react/issues/6731#issuecomment-254874553
const canSetTextContent = tag !== 'textarea' || nextProp !== '';
const canSetTextContent =
(!enableHostSingletons || tag !== 'body') &&
(tag !== 'textarea' || nextProp !== '');
if (canSetTextContent) {
setTextContent(domElement, nextProp);
}
} else if (typeof nextProp === 'number') {
setTextContent(domElement, '' + nextProp);
const canSetTextContent = !enableHostSingletons || tag !== 'body';
Comment on lines +317 to +323
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI, I'm not sure this is the best way to handle the situation with single-child-as-text but what's going on here is

  1. HostSingletons in beginWork never opt for single-child-as-text so they will always have a child fiber.
  2. When a singleton is acquired on mount we call setInitialProperties which assumes that if there is a single-child-as-text there won't be a child fiber.
  3. This is a subtle leak of internals into the host config but the body exclusion is because this is the only singleton that realistically can have a single-child-as-text and we don't want to set the text content here otherwise we'll end up with two copies (once for this insert, and once when the child fiber appends)

as an alternative I could always resetTextContent in the reconciler since the reconciler knows that there should never be this shortcut
OR I could support single-child-as-text for singletons

if (canSetTextContent) {
setTextContent(domElement, '' + nextProp);
}
}
} else if (
propKey === SUPPRESS_CONTENT_EDITABLE_WARNING ||
Expand Down
27 changes: 18 additions & 9 deletions packages/react-dom-bindings/src/client/ReactDOMComponentTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ import type {
import {
HostComponent,
HostResource,
HostSingleton,
HostText,
HostRoot,
SuspenseComponent,
} from 'react-reconciler/src/ReactWorkTags';

import {getParentSuspenseInstance} from './ReactDOMHostConfig';

import {enableScopeAPI, enableFloat} from 'shared/ReactFeatureFlags';
import {
enableScopeAPI,
enableFloat,
enableHostSingletons,
} from 'shared/ReactFeatureFlags';

const randomKey = Math.random()
.toString(36)
Expand Down Expand Up @@ -169,12 +174,14 @@ export function getInstanceFromNode(node: Node): Fiber | null {
(node: any)[internalInstanceKey] ||
(node: any)[internalContainerInstanceKey];
if (inst) {
const tag = inst.tag;
if (
inst.tag === HostComponent ||
inst.tag === HostText ||
inst.tag === SuspenseComponent ||
inst.tag === HostRoot ||
(enableFloat ? inst.tag === HostResource : false)
tag === HostComponent ||
tag === HostText ||
tag === SuspenseComponent ||
(enableFloat ? tag === HostResource : false) ||
(enableHostSingletons ? tag === HostSingleton : false) ||
tag === HostRoot
) {
return inst;
} else {
Expand All @@ -189,10 +196,12 @@ export function getInstanceFromNode(node: Node): Fiber | null {
* DOM node.
*/
export function getNodeFromInstance(inst: Fiber): Instance | TextInstance {
const tag = inst.tag;
if (
inst.tag === HostComponent ||
inst.tag === HostText ||
(enableFloat ? inst.tag === HostResource : false)
tag === HostComponent ||
(enableFloat ? tag === HostResource : false) ||
(enableHostSingletons ? tag === HostSingleton : false) ||
tag === HostText
) {
// In Fiber this, is just the state node right now. We assume it will be
// a host component or host text.
Expand Down
Loading