Skip to content

Commit

Permalink
Suspend Thenable/Lazy if it's used in React.Children and unwrap (#28284)
Browse files Browse the repository at this point in the history
This pains me because `React.Children` is really already
pseudo-deprecated.

`React.Children` takes any children that `React.Node` takes. We now
support Lazy and Thenable in this position elsewhere, but it errors in
`React.Children`.

This becomes an issue with async Server Components which can resolve
into a Lazy and in the future Lazy will just become Thenables. Which
causes this to error.

There are a few different semantics we could have:

1) Error like we already do (#28280). `React.Children` is about
introspecting children. It was always sketchy because you can't
introspect inside an abstraction anyway. With Server Components we fold
away the components so you can actually introspect inside of them kind
of but what they do is an implementation detail and you should be able
to turn it into a Client Component at any point. The type of an Element
passing the boundary actually reduces to `React.Node`.
2) Suspend and unwrap the Node (this PR). If we assume that Children is
called inside of render, then throwing a Promise if it's not already
loaded or unwrapping would treat it as if it wasn't there. Just like if
you rendered it in React. This lets you introspect what's inside which
isn't really something you should be able to do. This isn't compatible
with deprecating throwing-a-Promise and enable static compilation like
`use()` does. We'd have to deprecate `React.Children` before doing that
which we might anyway.
3) Wrap in a Fragment. If a Server Component was instead a Client
Component, you couldn't introspect through it anyway. Another
alternative might be to let it pass through but then it wouldn't be
given a flat key. We could also wrap it in a Fragment that is keyed.
That way you're always seeing an element. The issue with this solution
is that it wouldn't see the key of the Server Component since that gets
forwarded to the child that is yet to resolve. The nice thing about that
strategy is it doesn't depend on throw-a-Promise but it might not be
keyed correctly when things move.

DiffTrain build for commit 9e7944f.
  • Loading branch information
sebmarkbage committed Feb 12, 2024
1 parent 0bd6d70 commit 1739b31
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<1aed0b993a4c164c11bd44255ec4ddac>>
* @generated SignedSource<<9e5c6574214265b8a7f68fb3d0ee99e5>>
*/

"use strict";
Expand Down Expand Up @@ -5021,20 +5021,20 @@ if (__DEV__) {
rejectedThenable.reason = error;
}
}
); // Check one more time in case the thenable resolved synchronously.
);
} // Check one more time in case the thenable resolved synchronously.

switch (thenable.status) {
case "fulfilled": {
var fulfilledThenable = thenable;
return fulfilledThenable.value;
}
switch (thenable.status) {
case "fulfilled": {
var fulfilledThenable = thenable;
return fulfilledThenable.value;
}

case "rejected": {
var rejectedThenable = thenable;
var _rejectedError = rejectedThenable.reason;
checkIfUseWrappedInAsyncCatch(_rejectedError);
throw _rejectedError;
}
case "rejected": {
var rejectedThenable = thenable;
var _rejectedError = rejectedThenable.reason;
checkIfUseWrappedInAsyncCatch(_rejectedError);
throw _rejectedError;
}
} // Suspend.
//
Expand Down Expand Up @@ -25721,7 +25721,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-canary-629541bcc-20240212";
var ReactVersion = "18.3.0-canary-9e7944f67-20240212";

// Might add PROFILE later.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<1fafe8c779f4832b19959f28b49afe3e>>
* @generated SignedSource<<6223b3402ebe91d202065e784f2d9af4>>
*/

"use strict";
Expand Down Expand Up @@ -1402,16 +1402,16 @@ function trackUsedThenable(thenableState, thenable, index) {
}
}
);
switch (thenable.status) {
case "fulfilled":
return thenable.value;
case "rejected":
throw (
((thenableState = thenable.reason),
checkIfUseWrappedInAsyncCatch(thenableState),
thenableState)
);
}
}
switch (thenable.status) {
case "fulfilled":
return thenable.value;
case "rejected":
throw (
((thenableState = thenable.reason),
checkIfUseWrappedInAsyncCatch(thenableState),
thenableState)
);
}
suspendedThenable = thenable;
throw SuspenseException;
Expand Down Expand Up @@ -9173,7 +9173,7 @@ var devToolsConfig$jscomp$inline_1023 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-629541bcc-20240212",
version: "18.3.0-canary-9e7944f67-20240212",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1204 = {
Expand Down Expand Up @@ -9204,7 +9204,7 @@ var internals$jscomp$inline_1204 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-629541bcc-20240212"
reconcilerVersion: "18.3.0-canary-9e7944f67-20240212"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1205 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<a0a2d876cbe4fb943cdc49c02ecf6a31>>
* @generated SignedSource<<9a90acaf1cc6a5a23ed59dfb8d38c84f>>
*/

"use strict";
Expand Down Expand Up @@ -1422,16 +1422,16 @@ function trackUsedThenable(thenableState, thenable, index) {
}
}
);
switch (thenable.status) {
case "fulfilled":
return thenable.value;
case "rejected":
throw (
((thenableState = thenable.reason),
checkIfUseWrappedInAsyncCatch(thenableState),
thenableState)
);
}
}
switch (thenable.status) {
case "fulfilled":
return thenable.value;
case "rejected":
throw (
((thenableState = thenable.reason),
checkIfUseWrappedInAsyncCatch(thenableState),
thenableState)
);
}
suspendedThenable = thenable;
throw SuspenseException;
Expand Down Expand Up @@ -9601,7 +9601,7 @@ var devToolsConfig$jscomp$inline_1065 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-629541bcc-20240212",
version: "18.3.0-canary-9e7944f67-20240212",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1245 = {
Expand Down Expand Up @@ -9632,7 +9632,7 @@ var internals$jscomp$inline_1245 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-629541bcc-20240212"
reconcilerVersion: "18.3.0-canary-9e7944f67-20240212"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1246 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<70ab383eec7c39690b53a887a16d26d0>>
* @generated SignedSource<<e3defaed524124e7d1cb2b9785a29b43>>
*/

"use strict";
Expand All @@ -24,7 +24,7 @@ if (__DEV__) {
) {
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
}
var ReactVersion = "18.3.0-canary-629541bcc-20240212";
var ReactVersion = "18.3.0-canary-9e7944f67-20240212";

// ATTENTION
// When adding new symbols to this file,
Expand Down Expand Up @@ -1768,6 +1768,69 @@ if (__DEV__) {
return index.toString(36);
}

function noop() {}

function resolveThenable(thenable) {
switch (thenable.status) {
case "fulfilled": {
var fulfilledValue = thenable.value;
return fulfilledValue;
}

case "rejected": {
var rejectedError = thenable.reason;
throw rejectedError;
}

default: {
if (typeof thenable.status === "string") {
// Only instrument the thenable if the status if not defined. If
// it's defined, but an unknown value, assume it's been instrumented by
// some custom userspace implementation. We treat it as "pending".
// Attach a dummy listener, to ensure that any lazy initialization can
// happen. Flight lazily parses JSON when the value is actually awaited.
thenable.then(noop, noop);
} else {
// This is an uncached thenable that we haven't seen before.
// TODO: Detect infinite ping loops caused by uncached promises.
var pendingThenable = thenable;
pendingThenable.status = "pending";
pendingThenable.then(
function (fulfilledValue) {
if (thenable.status === "pending") {
var fulfilledThenable = thenable;
fulfilledThenable.status = "fulfilled";
fulfilledThenable.value = fulfilledValue;
}
},
function (error) {
if (thenable.status === "pending") {
var rejectedThenable = thenable;
rejectedThenable.status = "rejected";
rejectedThenable.reason = error;
}
}
);
} // Check one more time in case the thenable resolved synchronously.

switch (thenable.status) {
case "fulfilled": {
var fulfilledThenable = thenable;
return fulfilledThenable.value;
}

case "rejected": {
var rejectedThenable = thenable;
var _rejectedError = rejectedThenable.reason;
throw _rejectedError;
}
}
}
}

throw thenable;
}

function mapIntoArray(children, array, escapedPrefix, nameSoFar, callback) {
var type = typeof children;

Expand Down Expand Up @@ -1795,9 +1858,14 @@ if (__DEV__) {
break;

case REACT_LAZY_TYPE:
throw new Error(
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
"We recommend not iterating over children and just rendering them plain."
var payload = children._payload;
var init = children._init;
return mapIntoArray(
init(payload),
array,
escapedPrefix,
nameSoFar,
callback
);
}
}
Expand Down Expand Up @@ -1910,16 +1978,17 @@ if (__DEV__) {
);
}
} else if (type === "object") {
// eslint-disable-next-line react-internal/safe-string-coercion
var childrenString = String(children);

if (typeof children.then === "function") {
throw new Error(
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
"We recommend not iterating over children and just rendering them plain."
return mapIntoArray(
resolveThenable(children),
array,
escapedPrefix,
nameSoFar,
callback
);
}
} // eslint-disable-next-line react-internal/safe-string-coercion

var childrenString = String(children);
throw new Error(
"Objects are not valid as a React child (found: " +
(childrenString === "[object Object]"
Expand Down
Loading

0 comments on commit 1739b31

Please sign in to comment.