docs/get-started/frameworks/react-native-web-vite.mdx
Storybook for React Native Web is a framework that makes it easy to develop and test UI components in isolation for React Native. It uses Vite to build your components for web browsers.
<Callout variant="info"> In addition to React Native Web, Storybook supports on-device [React Native](https://github.com/storybookjs/react-native) development. If you're unsure what's right for you, read our [comparison](#react-native-vs-react-native-web). </Callout>To install Storybook in an existing React Native project, run this command in your project's root directory:
<CodeSnippets path="create-command.md" variant="new-users" copyEvent="CreateCommandCopy" />You can then get started writing stories, running tests and documenting your components. For more control over the installation process, refer to the installation guide.
<GetStartedVersions versions={[{ name: 'React Native', range: '≥ 0.72', icon: '/images/logos/renderers/logo-react.svg' }, { name: 'React Native Web', range: '≥ 0.19', icon: '/images/logos/renderers/logo-react.svg' }, { name: 'Vite', range: '≥ 5', icon: '/images/logos/builders/vite.svg' }]} />
To run Storybook for a particular project, run the following:
<CodeSnippets path="storybook-run-dev.md" />To build Storybook, run:
<CodeSnippets path="build-storybook-production-mode.md" />You will find the output in the configured outputDir (default is storybook-static).
If you’re building React Native (RN) components, Storybook has two options: Native and Web.
Both options provide a catalog of your stories that hot refreshes as you edit the code in your favorite editor. However, their implementations are quite different:
So, which option is right for you?
Native. You should choose this option if you want:
react-native-web, which works for most components but has limitations.Web. You should choose this option if you want:
Both. It’s also possible to use both options together. This increases Storybook’s install footprint but is a good option if you want native fidelity in addition to all of the web features. Learn more below.
The easiest way to use React Native and React Native Web is to select the "Both" option when installing Storybook. This will install and create configurations for both environments, allowing you to run Storybook for both in the same project.
When you select "Both", the installation will:
After installation, you'll see instructions for both environments:
storybook commandHowever, you can install them separately if one version is installed. You can add a React Native Web Storybook alongside an existing React Native Storybook by running the install command and selecting "React Native Web" in the setup wizard, and vice versa.
The React Native Web addon was a Webpack-based precursor to the React Native Web Vite framework (i.e., @storybook/react-native-web-vite). If you're using the addon, you should migrate to the framework, which is faster, more stable, maintained, and better documented. To do so, follow the steps below.
Run the following command to upgrade Storybook to the latest version:
<CodeSnippets path="storybook-upgrade.md" /> <Callout variant="info"> This framework is designed to work with Storybook 8.5 and above for the best experience. We won't be able to provide support if you're using an older Storybook version. </Callout>Install the framework and its peer dependencies:
<CodeSnippets path="react-native-web-vite-install.md" />Update your .storybook/main.js|ts to change the framework property and remove the @storybook/addon-react-native-web addon:
Finally, remove the addon and similar packages (i.e., @storybook/react-webpack5 and @storybook/addon-react-native-web) from your project.
You can pass an options object for additional configuration if needed:
import type { StorybookConfig } from '@storybook/react-native-web-vite';
const config: StorybookConfig = {
framework: {
name: '@storybook/react-native-web-vite',
options: {
modulesToTranspile: ['my-library'], // add libraries that are not transpiled for web by default
// You should apply babel plugins and presets here for your project that you want to apply to your code
// for example put the reanimated preset here if you are using reanimated
// or the nativewind jsxImportSource for example
pluginReactOptions: {
jsxRuntime: 'automatic' | 'classic', // default: 'automatic'
jsxImportSource: string, // default: 'react'
babel:{
plugins: Array<string | [string, any]>,
presets: Array<string | [string, any]>,
// ... other compatible babel options
}
include: Array<string|RegExp>,
exclude: Array<string|RegExp>,
// ... other compatible @vitejs/plugin-react options
}
},
},
};
export default config;
const config: StorybookConfig = {
// ... rest of config
framework: {
name: "@storybook/react-native-web-vite",
options: {
pluginReactOptions: {
babel: {
plugins: [
"@babel/plugin-proposal-export-namespace-from",
"react-native-reanimated/plugin",
],
},
},
},
},
// ... rest of config
}
const config: StorybookConfig = {
// ... rest of config
framework: {
name: "@storybook/react-native-web-vite",
options: {
pluginReactOptions: {
jsxImportSource: "nativewind",
},
},
},
}
Let's say you need to transpile a library called my-library that is not transpiled for web by default.
You can add it to the modulesToTranspile option.
const config: StorybookConfig = {
// ... rest of config
framework: {
name: "@storybook/react-native-web-vite",
options: {
modulesToTranspile: ['my-library'],
},
},
}
builderType: Record<string, any>
Configure options for the framework's builder. For this framework, available options can be found in the Vite builder docs.