setup-react-native-storybook by storybookjs/react-native
npx skills add https://github.com/storybookjs/react-native --skill setup-react-native-storybook将 @storybook/react-native v10 添加到 React Native 项目。
重要提示: 检测项目的包管理器(查找 yarn.lock、pnpm-lock.yaml 或 bun.lockb),并在所有安装/运行命令中使用它,而不是 npm。下面的示例使用 npm,但请相应替换(例如,使用 yarn add 代替 npm install,使用 yarn storybook 代替 npm run storybook)。对于 Expo 项目,使用 (或 等)来安装依赖项,以便 Expo 可以解析兼容的版本。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
npx expo installbunx expo install对于初始化命令,请使用 <pm> create storybook 并加上下面显示的标志。只有 npm 需要在标志前加上 --。切勿为此使用 npx/bunx 等。
根据项目类型,有四种设置流程:
rspack.config 或 webpack.config 并使用 @callstack/repack -> Re.Pack_layout.tsx 的 app/ 目录并使用 expo-router -> Expo Router@react-native-community/cli 且没有 Expo -> React Native CLInpm create storybook -- --type react_native --yes
# 或:pnpm create storybook --type react_native --yes
# 或:bun create storybook --type react_native --yes
这将安装依赖项并创建包含 main.ts、preview.tsx 和 index.tsx 的 .rnstorybook/ 目录。
更新生成的 .rnstorybook/index.tsx 以启用 WebSocket 支持。这是远程控制和与 Storybook Web 配套工具同步所必需的:
// .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-storage,请安装它:
npm install @react-native-async-storage/async-storage
CLI 在 .rnstorybook/main.ts 中生成了默认的 stories 匹配模式。保留现有的匹配模式,并添加一个指向项目中 UI 组件实际存放位置的额外条目。查找诸如 components/、src/components/、src/、ui/ 等目录:
// .rnstorybook/main.ts
const main: StorybookConfig = {
stories: [
'./stories/**/*.stories.?(ts|tsx|js|jsx)', // 初始化生成的示例 stories
'../src/components/**/*.stories.?(ts|tsx|js|jsx)', // 根据项目结构添加
],
// ...
};
对于 Metro 项目,使用 withStorybook 包装 metro 配置。对于 Re.Pack 项目,将 StorybookPlugin 添加到你的 rspack/webpack 配置中。详情请参阅相关的参考文件。
Storybook 的渲染方式因流程而异 - 请参阅相关的参考文件。
npm run start
npm run ios # 或 npm run android
module.exports = withStorybook(config, {
enabled: true, // 当为 false 时,从打包文件中移除 Storybook
configPath: './.rnstorybook', // 可选,当使用默认的 ./.rnstorybook 文件夹时是冗余的
useJs: false, // 生成 .js 文件而不是 .ts
docTools: true, // 自动参数提取
liteMode: false, // 模拟默认 UI 依赖项(与 react-native-ui-lite 一起使用)
websockets: { port: 7007, host: 'localhost' }, // 远程控制
});
new StorybookPlugin({
enabled: true, // 当为 false 时,从打包文件中剥离 Storybook
configPath: './.rnstorybook', // 可选,当使用默认的 ./.rnstorybook 文件夹时是冗余的
useJs: false, // 生成 .js 文件而不是 .ts
docTools: true, // 自动参数提取
liteMode: false, // 模拟默认 UI 依赖项(与 react-native-ui-lite 一起使用)
websockets: 'auto', // 'auto' 检测局域网 IP,或 { port: 7007, host: 'localhost' }
});
每周安装量
140
代码仓库
GitHub 星标数
1.3K
首次出现
2026年2月2日
安全审计
已安装于
codex125
gemini-cli123
opencode123
github-copilot120
kimi-cli116
amp116
Add @storybook/react-native v10 to a React Native project.
Important: Detect the project's package manager (look for yarn.lock, pnpm-lock.yaml, or bun.lockb) and use it for all install/run commands instead of npm. The examples below use npm but substitute accordingly (e.g. yarn add instead of npm install, yarn storybook instead of npm run storybook). For Expo projects, use npx expo install (or bunx expo install, etc.) to install dependencies so Expo can resolve compatible versions.
For the init command, use <pm> create storybook with the flags shown below. Only npm needs -- before the flags. Never use npx/bunx etc for this.
Four setup flows based on project type:
rspack.config or webpack.config and uses @callstack/repack -> Re.Packapp/ directory with _layout.tsx and uses expo-router -> Expo Router@react-native-community/cli with no Expo -> React Native CLInpm create storybook -- --type react_native --yes
# or: pnpm create storybook --type react_native --yes
# or: bun create storybook --type react_native --yes
This installs dependencies and creates .rnstorybook/ with main.ts, preview.tsx, and index.tsx.
Update the generated .rnstorybook/index.tsx to enable WebSocket support. This is required for remote control and syncing with the Storybook web companion:
// .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 @react-native-async-storage/async-storage, install it:
npm install @react-native-async-storage/async-storage
The CLI generates a default stories glob in .rnstorybook/main.ts. Keep the existing glob and add an additional entry pointing to where UI components actually live in the project. Look for directories like components/, src/components/, src/, ui/, etc.:
// .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
],
// ...
};
For Metro projects, wrap the metro config with withStorybook. For Re.Pack projects, add the StorybookPlugin to your rspack/webpack config. See the relevant reference file for details.
How Storybook is rendered differs per flow - see the relevant reference file.
npm run start
npm run ios # or npm run android
module.exports = withStorybook(config, {
enabled: true, // Remove Storybook from bundle when false
configPath: './.rnstorybook', // Optional and redundant when using the default ./.rnstorybook folder
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
});
new StorybookPlugin({
enabled: true, // Strip Storybook from bundle when false
configPath: './.rnstorybook', // Optional and redundant when using the default ./.rnstorybook folder
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' }
});
Weekly Installs
140
Repository
GitHub Stars
1.3K
First Seen
Feb 2, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex125
gemini-cli123
opencode123
github-copilot120
kimi-cli116
amp116
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装
Sonos CLI 命令行工具:本地网络控制 Sonos 音箱,支持分组、收藏、Spotify 搜索
708 周安装
SwiftData 教程:iOS 26+ 数据持久化、查询与管理完整指南
709 周安装
Lightpanda 无头浏览器 - Zig 语言构建,AI 自动化与网页抓取利器,速度提升11倍
693 周安装
Apple端侧AI开发指南:Core ML、Foundation Models、MLX Swift与llama.cpp选择与优化
699 周安装
AutoResearchClaw:全自动AI研究流水线,从主题到完整学术论文(含LaTeX格式)
697 周安装
Vue Pinia单元测试指南:组件、组合函数与状态管理测试最佳实践
711 周安装