setup-react-native-storybook
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Native Storybook Setup
React Native Storybook 配置指南
Add v10 to a React Native project.
@storybook/react-nativeImportant: Detect the project's package manager (look for , , or ) and use it for all install/run commands instead of . The examples below use but substitute accordingly (e.g. instead of , instead of ). For Expo projects, use (or , etc.) to install dependencies so Expo can resolve compatible versions.
yarn.lockpnpm-lock.yamlbun.lockbnpmnpmyarn addnpm installyarn storybooknpm run storybooknpx expo installbunx expo installFor the init command, use with the flags shown below. Only npm needs before the flags. Never use / etc for this.
<pm> create storybook--npxbunxFour setup flows based on project type:
- Expo (no router) - see references/expo-setup.md
- Expo with Expo Router - see references/expo-router-setup.md
- React Native CLI (no Expo) - see references/react-native-cli-setup.md
- Re.Pack (rspack/webpack) - see references/repack-setup.md
在React Native项目中添加 v10版本。
@storybook/react-native重要提示: 检测项目的包管理器(查找、或文件),并使用该包管理器执行所有安装/运行命令,而非。以下示例使用,请根据实际情况替换(例如用替代,用替代)。对于Expo项目,请使用(或等)安装依赖,以便Expo解析兼容版本。
yarn.lockpnpm-lock.yamlbun.lockbnpmnpmyarn addnpm installyarn storybooknpm run storybooknpx expo installbunx expo install初始化命令请使用并添加下方所示的参数。仅npm需要在参数前添加。请勿使用/等工具执行此命令。
<pm> create storybook--npxbunx根据项目类型分为四种配置流程:
- Expo(无路由) - 查看references/expo-setup.md
- Expo + Expo Router - 查看references/expo-router-setup.md
- React Native CLI(无Expo) - 查看references/react-native-cli-setup.md
- Re.Pack(rspack/webpack) - 查看references/repack-setup.md
Flow Selection
流程选择规则
- Project has or
rspack.configand useswebpack.config-> Re.Pack@callstack/repack - Project has directory with
app/and uses_layout.tsx-> Expo Routerexpo-router - Project uses Expo but not file-based routing -> Expo
- Project uses with no Expo -> React Native CLI
@react-native-community/cli
- 项目包含或
rspack.config且使用webpack.config-> Re.Pack流程@callstack/repack - 项目包含目录及
app/文件且使用_layout.tsx-> Expo Router流程expo-router - 项目使用Expo但未采用文件路由 -> Expo流程
- 项目使用且未集成Expo -> React Native CLI流程
@react-native-community/cli
Common Steps (all flows)
通用步骤(所有流程)
1. Run CLI Init
1. 运行CLI初始化命令
bash
npm create storybook -- --type react_native --yesbash
npm create storybook -- --type react_native --yesor: pnpm create storybook --type react_native --yes
或: pnpm create storybook --type react_native --yes
or: bun create storybook --type react_native --yes
或: bun create storybook --type react_native --yes
This installs dependencies and creates `.rnstorybook/` with `main.ts`, `preview.tsx`, and `index.tsx`.
该命令会安装依赖并创建`.rnstorybook/`目录,包含`main.ts`、`preview.tsx`和`index.tsx`文件。2. Enable WebSockets in .rnstorybook/index.tsx
2. 在.rnstorybook/index.tsx中启用WebSocket
Update the generated to enable WebSocket support. This is required for remote control and syncing with the Storybook web companion:
.rnstorybook/index.tsxtsx
// .rnstorybook/index.tsx
import AsyncStorage from '@react-native-async-storage/async-storage';
import { view } from './storybook.requires';
const StorybookUIRoot = view.getStorybookUI({
storage: {
getItem: AsyncStorage.getItem,
setItem: AsyncStorage.setItem,
},
enableWebsockets: true,
});
export default StorybookUIRoot;If the project doesn't have , install it:
@react-native-async-storage/async-storagebash
npm install @react-native-async-storage/async-storage更新生成的文件以启用WebSocket支持,这是实现远程控制及与Storybook网页端同步的必要配置:
.rnstorybook/index.tsxtsx
// .rnstorybook/index.tsx
import AsyncStorage from '@react-native-async-storage/async-storage';
import { view } from './storybook.requires';
const StorybookUIRoot = view.getStorybookUI({
storage: {
getItem: AsyncStorage.getItem,
setItem: AsyncStorage.setItem,
},
enableWebsockets: true,
});
export default StorybookUIRoot;若项目未安装,请执行以下命令安装:
@react-native-async-storage/async-storagebash
npm install @react-native-async-storage/async-storage3. Update Story Globs in main.ts
3. 在main.ts中更新Story文件匹配规则
The CLI generates a default glob in . Keep the existing glob and add an additional entry pointing to where UI components actually live in the project. Look for directories like , , , , etc.:
stories.rnstorybook/main.tscomponents/src/components/src/ui/ts
// .rnstorybook/main.ts
const main: StorybookConfig = {
stories: [
'./stories/**/*.stories.?(ts|tsx|js|jsx)', // example stories from init
'../src/components/**/*.stories.?(ts|tsx|js|jsx)', // add based on project structure
],
// ...
};CLI会在中生成默认的stories匹配规则。保留现有规则并添加一条指向项目UI组件实际存放目录的规则。常见组件目录包括、、、等:
.rnstorybook/main.tscomponents/src/components/src/ui/ts
// .rnstorybook/main.ts
const main: StorybookConfig = {
stories: [
'./stories/**/*.stories.?(ts|tsx|js|jsx)', // 初始化时生成的示例stories
'../src/components/**/*.stories.?(ts|tsx|js|jsx)', // 根据项目结构添加
],
// ...
};4. Configure Bundler
4. 配置打包工具
For Metro projects, wrap the metro config with . For Re.Pack projects, add the to your rspack/webpack config. See the relevant reference file for details.
withStorybookStorybookPlugin对于Metro项目,使用包装Metro配置。对于Re.Pack项目,在rspack/webpack配置中添加。详情请查看对应流程的参考文档。
withStorybookStorybookPlugin5. Create Entrypoint
5. 创建入口文件
How Storybook is rendered differs per flow - see the relevant reference file.
不同流程中Storybook的渲染方式有所差异,请查看对应流程的参考文档。
6. Run
6. 运行项目
bash
npm run start
npm run ios # or npm run androidbash
npm run start
npm run ios # 或 npm run androidwithStorybook Options (Metro)
withStorybook配置项(Metro)
js
module.exports = withStorybook(config, {
enabled: true, // Remove Storybook from bundle when false
configPath: './.rnstorybook', // Storybook config directory
useJs: false, // Generate .js instead of .ts
docTools: true, // Auto arg extraction
liteMode: false, // Mock default UI deps (use with react-native-ui-lite)
websockets: { port: 7007, host: 'localhost' }, // Remote control
});js
module.exports = withStorybook(config, {
enabled: true, // 设置为false时会从打包文件中移除Storybook
configPath: './.rnstorybook', // Storybook配置目录路径
useJs: false, // 生成.js文件而非.ts文件
docTools: true, // 自动提取参数
liteMode: false, // 模拟默认UI依赖(配合react-native-ui-lite使用)
websockets: { port: 7007, host: 'localhost' }, // 远程控制配置
});StorybookPlugin Options (Re.Pack)
StorybookPlugin配置项(Re.Pack)
js
new StorybookPlugin({
enabled: true, // Strip Storybook from bundle when false
configPath: './.rnstorybook', // Storybook config directory
useJs: false, // Generate .js instead of .ts
docTools: true, // Auto arg extraction
liteMode: false, // Mock default UI deps (use with react-native-ui-lite)
websockets: 'auto', // 'auto' detects LAN IP, or { port: 7007, host: 'localhost' }
});js
new StorybookPlugin({
enabled: true, // 设置为false时会从打包文件中移除Storybook
configPath: './.rnstorybook', // Storybook配置目录路径
useJs: false, // 生成.js文件而非.ts文件
docTools: true, // 自动提取参数
liteMode: false, // 模拟默认UI依赖(配合react-native-ui-lite使用)
websockets: 'auto', // 'auto'会自动检测局域网IP,也可手动配置为{ port: 7007, host: 'localhost' }
});