Skip to content

Commit 23457ab

Browse files
committed
Suspend Thenable/Lazy if it's used in React.Children and unwrap (#28284)
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 [9e7944f](9e7944f)
1 parent c4aeba9 commit 23457ab

25 files changed

+816
-410
lines changed

compiled/facebook-www/REVISION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
629541bcc09fc7c0cc5c257541d084ee27457512
1+
9e7944f67c72b9a69a8db092ba6bd99fe9c731e2

compiled/facebook-www/React-dev.classic.js

+80-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (__DEV__) {
2424
) {
2525
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
2626
}
27-
var ReactVersion = "18.3.0-www-classic-98f29e71";
27+
var ReactVersion = "18.3.0-www-classic-ab1e0612";
2828

2929
// ATTENTION
3030
// When adding new symbols to this file,
@@ -2113,6 +2113,69 @@ if (__DEV__) {
21132113
return index.toString(36);
21142114
}
21152115

2116+
function noop$1() {}
2117+
2118+
function resolveThenable(thenable) {
2119+
switch (thenable.status) {
2120+
case "fulfilled": {
2121+
var fulfilledValue = thenable.value;
2122+
return fulfilledValue;
2123+
}
2124+
2125+
case "rejected": {
2126+
var rejectedError = thenable.reason;
2127+
throw rejectedError;
2128+
}
2129+
2130+
default: {
2131+
if (typeof thenable.status === "string") {
2132+
// Only instrument the thenable if the status if not defined. If
2133+
// it's defined, but an unknown value, assume it's been instrumented by
2134+
// some custom userspace implementation. We treat it as "pending".
2135+
// Attach a dummy listener, to ensure that any lazy initialization can
2136+
// happen. Flight lazily parses JSON when the value is actually awaited.
2137+
thenable.then(noop$1, noop$1);
2138+
} else {
2139+
// This is an uncached thenable that we haven't seen before.
2140+
// TODO: Detect infinite ping loops caused by uncached promises.
2141+
var pendingThenable = thenable;
2142+
pendingThenable.status = "pending";
2143+
pendingThenable.then(
2144+
function (fulfilledValue) {
2145+
if (thenable.status === "pending") {
2146+
var fulfilledThenable = thenable;
2147+
fulfilledThenable.status = "fulfilled";
2148+
fulfilledThenable.value = fulfilledValue;
2149+
}
2150+
},
2151+
function (error) {
2152+
if (thenable.status === "pending") {
2153+
var rejectedThenable = thenable;
2154+
rejectedThenable.status = "rejected";
2155+
rejectedThenable.reason = error;
2156+
}
2157+
}
2158+
);
2159+
} // Check one more time in case the thenable resolved synchronously.
2160+
2161+
switch (thenable.status) {
2162+
case "fulfilled": {
2163+
var fulfilledThenable = thenable;
2164+
return fulfilledThenable.value;
2165+
}
2166+
2167+
case "rejected": {
2168+
var rejectedThenable = thenable;
2169+
var _rejectedError = rejectedThenable.reason;
2170+
throw _rejectedError;
2171+
}
2172+
}
2173+
}
2174+
}
2175+
2176+
throw thenable;
2177+
}
2178+
21162179
function mapIntoArray(children, array, escapedPrefix, nameSoFar, callback) {
21172180
var type = typeof children;
21182181

@@ -2140,9 +2203,14 @@ if (__DEV__) {
21402203
break;
21412204

21422205
case REACT_LAZY_TYPE:
2143-
throw new Error(
2144-
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
2145-
"We recommend not iterating over children and just rendering them plain."
2206+
var payload = children._payload;
2207+
var init = children._init;
2208+
return mapIntoArray(
2209+
init(payload),
2210+
array,
2211+
escapedPrefix,
2212+
nameSoFar,
2213+
callback
21462214
);
21472215
}
21482216
}
@@ -2255,16 +2323,17 @@ if (__DEV__) {
22552323
);
22562324
}
22572325
} else if (type === "object") {
2258-
// eslint-disable-next-line react-internal/safe-string-coercion
2259-
var childrenString = String(children);
2260-
22612326
if (typeof children.then === "function") {
2262-
throw new Error(
2263-
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
2264-
"We recommend not iterating over children and just rendering them plain."
2327+
return mapIntoArray(
2328+
resolveThenable(children),
2329+
array,
2330+
escapedPrefix,
2331+
nameSoFar,
2332+
callback
22652333
);
2266-
}
2334+
} // eslint-disable-next-line react-internal/safe-string-coercion
22672335

2336+
var childrenString = String(children);
22682337
throw new Error(
22692338
"Objects are not valid as a React child (found: " +
22702339
(childrenString === "[object Object]"

compiled/facebook-www/React-dev.modern.js

+80-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (__DEV__) {
2424
) {
2525
__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
2626
}
27-
var ReactVersion = "18.3.0-www-modern-33733854";
27+
var ReactVersion = "18.3.0-www-modern-d9e56569";
2828

2929
// ATTENTION
3030
// When adding new symbols to this file,
@@ -2078,6 +2078,69 @@ if (__DEV__) {
20782078
return index.toString(36);
20792079
}
20802080

2081+
function noop$1() {}
2082+
2083+
function resolveThenable(thenable) {
2084+
switch (thenable.status) {
2085+
case "fulfilled": {
2086+
var fulfilledValue = thenable.value;
2087+
return fulfilledValue;
2088+
}
2089+
2090+
case "rejected": {
2091+
var rejectedError = thenable.reason;
2092+
throw rejectedError;
2093+
}
2094+
2095+
default: {
2096+
if (typeof thenable.status === "string") {
2097+
// Only instrument the thenable if the status if not defined. If
2098+
// it's defined, but an unknown value, assume it's been instrumented by
2099+
// some custom userspace implementation. We treat it as "pending".
2100+
// Attach a dummy listener, to ensure that any lazy initialization can
2101+
// happen. Flight lazily parses JSON when the value is actually awaited.
2102+
thenable.then(noop$1, noop$1);
2103+
} else {
2104+
// This is an uncached thenable that we haven't seen before.
2105+
// TODO: Detect infinite ping loops caused by uncached promises.
2106+
var pendingThenable = thenable;
2107+
pendingThenable.status = "pending";
2108+
pendingThenable.then(
2109+
function (fulfilledValue) {
2110+
if (thenable.status === "pending") {
2111+
var fulfilledThenable = thenable;
2112+
fulfilledThenable.status = "fulfilled";
2113+
fulfilledThenable.value = fulfilledValue;
2114+
}
2115+
},
2116+
function (error) {
2117+
if (thenable.status === "pending") {
2118+
var rejectedThenable = thenable;
2119+
rejectedThenable.status = "rejected";
2120+
rejectedThenable.reason = error;
2121+
}
2122+
}
2123+
);
2124+
} // Check one more time in case the thenable resolved synchronously.
2125+
2126+
switch (thenable.status) {
2127+
case "fulfilled": {
2128+
var fulfilledThenable = thenable;
2129+
return fulfilledThenable.value;
2130+
}
2131+
2132+
case "rejected": {
2133+
var rejectedThenable = thenable;
2134+
var _rejectedError = rejectedThenable.reason;
2135+
throw _rejectedError;
2136+
}
2137+
}
2138+
}
2139+
}
2140+
2141+
throw thenable;
2142+
}
2143+
20812144
function mapIntoArray(children, array, escapedPrefix, nameSoFar, callback) {
20822145
var type = typeof children;
20832146

@@ -2105,9 +2168,14 @@ if (__DEV__) {
21052168
break;
21062169

21072170
case REACT_LAZY_TYPE:
2108-
throw new Error(
2109-
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
2110-
"We recommend not iterating over children and just rendering them plain."
2171+
var payload = children._payload;
2172+
var init = children._init;
2173+
return mapIntoArray(
2174+
init(payload),
2175+
array,
2176+
escapedPrefix,
2177+
nameSoFar,
2178+
callback
21112179
);
21122180
}
21132181
}
@@ -2220,16 +2288,17 @@ if (__DEV__) {
22202288
);
22212289
}
22222290
} else if (type === "object") {
2223-
// eslint-disable-next-line react-internal/safe-string-coercion
2224-
var childrenString = String(children);
2225-
22262291
if (typeof children.then === "function") {
2227-
throw new Error(
2228-
"Cannot render an Async Component, Promise or React.Lazy inside React.Children. " +
2229-
"We recommend not iterating over children and just rendering them plain."
2292+
return mapIntoArray(
2293+
resolveThenable(children),
2294+
array,
2295+
escapedPrefix,
2296+
nameSoFar,
2297+
callback
22302298
);
2231-
}
2299+
} // eslint-disable-next-line react-internal/safe-string-coercion
22322300

2301+
var childrenString = String(children);
22332302
throw new Error(
22342303
"Objects are not valid as a React child (found: " +
22352304
(childrenString === "[object Object]"

0 commit comments

Comments
 (0)