<Suspense>
is supposed to be used in very specific manner. It suspends the rendering of lazy
components and renders other kinds of components as is.
Test
is not lazy
component but async
function, it returns promise object and not React element, hence Test
is not a valid React component.
lazy
components are aimed at rendering default imports from lazily loaded components. Basically lazy
component should return a promise of an object with default
property that contains valid React component.
It can be used as:
const Test = lazy(async () => {
const data = await fetch('https://jsonplaceholder.typicode.com/posts/42');
const json = await data.json();
const id = json.id;
return { default: (props) => <div>{id}</div> };
});
...
<Suspense fallback={<LoadingMessage />}>
<Test />
</Suspense>
Notice that due to its primary use case, lazy
component can't accept parameters, if Test
had props, they would be passed to a component that async
function returns.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…