Storybook Addon Remix React Router
在你的 stories 中使用 Remix React Router。
从 react-router@7
开始,Remix 已弃用包 react-router-dom
。
如果你仍在使用此包,请使用此插件的 v3
版本。
如果你已弃用它转而使用 react-router
,请根据你的 Storybook 版本使用 v4
或 v5
。
查看底部的兼容性表格。
最新变更
✅ 支持 Storybook 9,使用 storybook-addon-remix-react-router@5
。
✅ 支持 React Router v7,使用 storybook-addon-remix-react-router@4
。
✅ 支持 Storybook 8,使用 storybook-addon-remix-react-router@3
。
入门
安装包
npm i -D storybook-addon-remix-react-router
将其添加到你的 storybook 配置中
// .storybook/main.ts
export default {
addons: ['storybook-addon-remix-react-router'],
} satisfies StorybookConfig;
装饰组件的所有 stories
要将路由器添加到组件的所有 stories 中,只需将其添加到 decorators
数组即可。
请注意,parameters.reactRouter
是可选的,默认情况下路由器将在 /
处渲染组件。
import { withRouter, reactRouterParameters } from 'storybook-addon-remix-react-router';
export default {
title: 'User Profile',
render: () => <UserProfile />,
decorators: [withRouter],
parameters: {
reactRouter: reactRouterParameters({
location: {
pathParams: { userId: '42' },
},
routing: { path: '/users/:userId' },
}),
},
};
装饰特定的 story
要更改单个 story 的配置,你可以这样做
import { withRouter, reactRouterParameters } from 'storybook-addon-remix-react-router';
export default {
title: 'User Profile',
render: () => <UserProfile />,
decorators: [withRouter],
};
export const FromHomePage = {
parameters: {
reactRouter: reactRouterParameters({
location: {
pathParams: { userId: '42' },
searchParams: { tab: 'activityLog' },
state: { fromPage: 'homePage' },
},
routing: {
path: '/users/:userId',
handle: 'Profile',
},
}),
},
};
全局装饰所有 stories
通过在你的 preview.js
文件中添加装饰器,将项目的所有 stories 包裹在路由器中。
// .storybook/preview.js
export default {
decorators: [withRouter],
parameters: {
reactRouter: reactRouterParameters({ ... }),
}
} satisfies Preview;
Location
要指定与浏览器 location 相关的任何内容,请使用 location
属性。
type LocationParameters = {
path?: string | ((inferredPath: string, pathParams: Record<string, string>) => string | undefined);
pathParams?: PathParams;
searchParams?: ConstructorParameters<typeof URLSearchParams>[0];
hash?: string;
state?: unknown;
};
推断路径
如果未提供 location.path
,浏览器 pathname 将使用 routing
属性中的连接的 path
s 和 pathParams
生成。
作为函数的路径
你可以为 path
提供一个函数。
它将接收来自 routing 属性的连接的 path
s 和 pathParams
作为参数。
如果函数返回一个 string
,它将按原样使用。如果需要,由你决定是否调用 react-router
中的 generatePath
。
如果函数返回 undefined
,它将回退到默认行为,就像你没有为 location.path
提供任何值一样。
路由
你可以将 routing
设置为 createBrowserRouter
接受的任何内容。
为了让你的工作更轻松,storybook-addon-remix-react-router
附带了一些路由助手
export const MyStory = {
parameters: {
reactRouter: reactRouterParameters({
routing: reactRouterOutlet(<MyOutlet />),
}),
},
};
路由助手
以下助手是开箱即用的
reactRouterOutlet(); // Render a single outlet
reactRouterOutlets(); // Render multiple outlets
reactRouterNestedOutlets(); // Render multiple outlets nested one into another
reactRouterNestedAncestors(); // Render the story as an outlet of nested outlets
你也可以创建自己的助手,并使用导出的类型 RoutingHelper
来帮助你
import { RoutingHelper } from 'storybook-addon-remix-react-router';
const myCustomHelper: RoutingHelper = () => {
// Routing creation logic
};
RouterRoute
本质上是 react-router
中的原生 RouteObject
;增加了 { useStoryElement?: boolean }
。如果你想接受一个 JSX 并将其转换为 RouterRoute
,可以使用导出的函数 castRouterRoute
。
将 story 用作路由元素
只需在路由配置对象中设置 { useStoryElement: true }
。
专用面板
导航事件、loader 和 actions 会被记录,以便你更好地理解组件的生命周期。
兼容性
此包旨在支持 Storybook > 7
和 React > 16
。
这是一个兼容性表格
插件 | React | Storybook | React Router |
---|---|---|---|
5.x | >= 16.8.0 | 9.x | 7.x |
4.x | >= 16.8.0 | 8.x | 7.x |
3.x | >= 16.8.0 | 8.x | 6.x 1 |
2.x | >= 16.8.0 < 19.0.0 | 7.x | 6.x |
1.x | >= 16.8.0 < 19.0.0 | 7.x | 6.x |
1 实际上,如果你从 react-router-dom
而不是 react-router
导入,你可以使用 react-router v7。
如果你在使用任何版本时遇到问题,请提出 issue。
贡献
欢迎贡献。
在编写任何代码之前,请先提出一个 issue,以展示你想要在此插件中看到的 bug 或功能的用例。
许可证
此包根据 Apache 2.0 许可证发布。