@stackflow/plugin-preload
@stackflow/plugin-preload
is useful when you need to load remote data before rendering the activity.
Installation
npm install @stackflow/plugin-preload
Usage
stackflow.ts
import { stackflow } from "@stackflow/react";
import { preloadPlugin } from "@stackflow/plugin-preload";
import { MyHome } from "./MyHome";
import { MyArticle } from "./MyArticle";
import { NotFoundPage } from "./NotFoundPage";
const { Stack, useFlow, activities } = stackflow({
activities: {
MyHome,
MyArticle,
NotFoundPage,
},
plugins: [
// ...
preloadPlugin({
loaders: {
MyHome({ activityParams }) {
// implement your own preload function using activity information
// when activity pushed, loader is automatically triggered before rendering
},
MyArticle() {
// ...
},
NotFoundPage() {
// ...
},
},
}),
],
});
export type TypeActivities = typeof activities;
usePreloader.ts
import { createPreloader } from "@stackflow/plugin-preload";
import type { TypeActivities } from "./stackflow";
export const { usePreloader } = createPreloader<TypeActivities>();
MyComponent.tsx
import { usePreloader } from "./usePreloader";
const MyComponent = () => {
const { preload } = usePreloader();
useEffect(() => {
// imperatively preload
preload("MyArticle", {
/* ... */
});
}, []);
return <div>{/* ... */}</div>;
};
Reference
preloadPlugin
Option | Type | Description |
---|---|---|
loaders | { [key]: Loader } | A mapping of activity names to their respective loader functions, defining how to preload the activity's data or resources. |
usePreloader
Option | Type | Description |
---|---|---|
urlPatternOptions | UrlPatternOptions | Options for customizing URL pattern matching within the preloader function. |