autosuspense
v0.1.14
Published
Automatic suspense wiring block for React.
Maintainers
Readme
AutoSupense Beta
AutoSuspense is a small React utility that automatically builds and composes Suspense fallback UI based on your component tree without manually wiring nested fallback components.
How to use:
Install this library
npm i autosuspenseUse it by calling and passing your components normally like
<Supense>:
import { AutoSuspense } from "autosuspense";
function App() {
return (
<AutoSuspense>
<Page />
</AutoSuspense>
);
}- Inside each AutoSuspense component provide a fallback componet for that particular component via
useSuspenseFallbackhook.
import { useSuspenseFallback } from "autosuspense";
function UserCard() {
useSuspenseFallback(<div>Loading user…</div>);
// Component may suspend somewhere below
return <Profile />;
}Important Caveats:
- This library works on top of existing Suspense blocks and prebuilds your Fallback UI via render tree traversal.
- This means that it uses Depth First searching to prebuild so react is only able to show prebuilt Ui when it first encounters a place to suspend. This could mean that rest of your fallback UI in next adjacent components never render.
TBD:
- Adding Default Skeletons.
- Supporting existing Library skeleton implementations.
- Adding a central config & template extension components & file to easily extend and manage your Fallback UI.
