Usage with Storybook? #952
Replies: 9 comments 20 replies
-
Currently with Storybook I get this error on most stories: ReferenceError: Cannot access 'Root' before initialization |
Beta Was this translation helpful? Give feedback.
-
I would like to know how to use Storybook as well. I think a plugin would be great, or simply a working solution how to create the router context for stories that you can display components which includes any API of the router |
Beta Was this translation helpful? Give feedback.
-
I'm interested to know if there are any workaround/solutions to render the |
Beta Was this translation helpful? Give feedback.
-
Here's what I created: Storybook fake Tanstack router - Gist |
Beta Was this translation helpful? Give feedback.
-
Here is what I've done import type {PartialStoryFn, StoryContext} from '@storybook/types';
import {createMemoryHistory, createRootRoute, createRoute, createRouter, RouterProvider} from '@tanstack/react-router';
export default function withRouter(Story: PartialStoryFn, {parameters}: StoryContext) {
const {initialEntries = ['/'], initialIndex, routes = ['/']} = parameters?.router || {};
const rootRoute = createRootRoute();
const children = routes.map((path) =>
createRoute({
path,
getParentRoute: () => rootRoute,
component: Story,
}),
);
rootRoute.addChildren(children);
const router = createRouter({
history: createMemoryHistory({initialEntries, initialIndex}),
routeTree: rootRoute,
});
return <RouterProvider router={router} />;
}
declare module '@storybook/types' {
interface Parameters {
router?: {
initialEntries?: string[];
initialIndex?: number;
routes?: string[];
};
}
} You can add it as global decorator in |
Beta Was this translation helpful? Give feedback.
-
Here's something simple that's been working fine with router v1.4 and storybook v7.6. You can see it has no business logic at all, but it's enough to provide the router context so things don't break. // preview.tsx
import type { Preview } from '@storybook/react';
import { RouterProvider, createMemoryHistory, createRootRoute, createRouter } from '@tanstack/react-router';
const preview: Preview = {
decorators: [
(Story) => {
return <RouterProvider router={createRouter({
history: createMemoryHistory(),
routeTree: createRootRoute({
component: Story
})
})} />
}
]
};
export default preview |
Beta Was this translation helpful? Give feedback.
-
Just FYI, there's quite a bit of subtlety involved with the Storybook (8) integration, the simple solution I and others have pasted above does not always work properly when switching between stories:
|
Beta Was this translation helpful? Give feedback.
-
I bypassed the RouterProvider and used the RouterContextProvider and so far it works well. Can't promise it wouldn't break in a future version or that it works in all cases.
|
Beta Was this translation helpful? Give feedback.
-
I tried all the above ways but it all looses either strong typings on
I start to provide my own useSearch which basically keeps the same typings but replaces the options with {strict:false} import {
type AnyRoute,
type FullSearchSchema,
type RegisteredRouter,
type RouteById,
type RouteIds,
useSearch
} from "@tanstack/react-router"
import { isStorybook } from "../lib/generalConfig"
type StringLiteral<T> = T extends string ? (string extends T ? string : T) : never
type StrictOrFrom<TFrom, TStrict extends boolean = true> = TStrict extends false
? {
from?: never
strict: TStrict
}
: {
from: StringLiteral<TFrom> | TFrom
strict?: TStrict
}
type UseSearchOptions<TFrom, TStrict extends boolean, TSearch, TSelected> = StrictOrFrom<
TFrom,
TStrict
> & {
select?: (search: TSearch) => TSelected
}
type Expand<T> = T extends object
? T extends infer O
? O extends Function
? O
: { [K in keyof O]: O[K] }
: never
: T
/**
* Enhanced useSearch hook that optionally disables strict mode if running in Storybook.
*/
export function usePtSearch<
TRouteTree extends AnyRoute = RegisteredRouter["routeTree"],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TStrict extends boolean = true,
TSearch = TStrict extends false
? FullSearchSchema<TRouteTree>
: Expand<RouteById<TRouteTree, TFrom>["types"]["fullSearchSchema"]>,
TSelected = TSearch
>(opts: UseSearchOptions<TFrom, TStrict, TSearch, TSelected>): TSelected {
// Create a modified options object if in Storybook mode
// Call the useSearch hook with the appropriate options
return useSearch(isStorybook ? { strict: false } : opts)
} |
Beta Was this translation helpful? Give feedback.
-
Hi there,
We're currently using React-Router and considering migrating to Tanstack Router...
We use Storybook a lot and we needed to use an add-on to enable React Router to work with Storybook.
Is this also the case with Tanstack Router? and if so does such an add-on exist yet?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions