/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {
Fragment,
Suspense,
unstable_SuspenseList as SuspenseList,
useState,
} from 'react';
function SuspenseTree(): React.Node {
return (
Suspense
Primary to Fallback Cycle
Fallback to Primary Cycle
);
}
function EmptySuspense() {
return ;
}
// $FlowFixMe[missing-local-annot]
function PrimaryFallbackTest({initialSuspend}) {
const [suspend, setSuspend] = useState(initialSuspend);
const fallbackStep = useTestSequence('fallback', Fallback1, Fallback2);
const primaryStep = useTestSequence('primary', Primary1, Primary2);
return (
setSuspend(e.target.checked)}
type="checkbox"
/>
Suspend
{suspend ? : primaryStep}
);
}
function useTestSequence(label: string, T1: any => any, T2: any => any) {
const [step, setStep] = useState(0);
const next: $FlowFixMe = (
setStep(s => (s + 1) % allSteps.length)}>
next {label} content
);
const allSteps: $FlowFixMe = [
{next} ,
{next} mount
,
{next} update
,
{next} several different {' '}
children
,
{next} goodbye
,
];
return allSteps[step];
}
function NestedSuspenseTest() {
return (
Nested Suspense
Loading outer}>
);
}
function Parent() {
return (
Loading inner 1}>
Hello
{' '}
Loading inner 2}>
World
This will never load}>
);
}
function SuspenseListTest() {
return (
<>
SuspenseList
>
);
}
function LoadLater() {
const [loadChild, setLoadChild] = useState(false);
return (
setLoadChild(true)}>Click to load
}>
{loadChild ? (
setLoadChild(false)}>
Loaded! Click to suspend again.
) : (
)}
);
}
function Never() {
throw new Promise(resolve => {});
}
function Fallback1({prop, ...rest}: any) {
return ;
}
function Fallback2({prop, ...rest}: any) {
return ;
}
function Primary1({prop, ...rest}: any) {
return ;
}
function Primary2({prop, ...rest}: any) {
return ;
}
export default SuspenseTree;