What is @storybook/react-native?
@storybook/react-native is a tool for developing and testing React Native components in isolation. It allows developers to create and showcase UI components in a structured and interactive manner, making it easier to build and maintain a consistent design system.
What are @storybook/react-native's main functionalities?
Component Story Creation
Allows developers to create stories for different states of a component, making it easier to visualize and test various scenarios.
import { storiesOf } from '@storybook/react-native';
import React from 'react';
import { Button } from 'react-native';
storiesOf('Button', module)
.add('default', () => (
<Button title="Default Button" onPress={() => {}} />
))
.add('disabled', () => (
<Button title="Disabled Button" onPress={() => {}} disabled />
));
Addons Integration
Supports various addons like knobs, actions, and links to enhance the functionality of stories, providing more interactive and dynamic component testing.
import { addDecorator } from '@storybook/react-native';
import { withKnobs } from '@storybook/addon-knobs';
addDecorator(withKnobs);
Live Component Preview
Enables live preview of components on a device or emulator, allowing developers to see changes in real-time as they modify their components.
import { getStorybookUI } from '@storybook/react-native';
import { AppRegistry } from 'react-native';
import { name as appName } from './app.json';
const StorybookUIRoot = getStorybookUI({
asyncStorage: null,
});
AppRegistry.registerComponent(appName, () => StorybookUIRoot);
Other packages similar to @storybook/react-native
react-native-storybook-loader
A utility to automatically load stories for React Native Storybook. It simplifies the process of importing and organizing stories, but it does not provide the full suite of features that @storybook/react-native offers, such as addons and live previews.
react-native-snap-carousel
While not a direct competitor, this package allows for the creation of carousels in React Native. It can be used in conjunction with @storybook/react-native to showcase carousel components, but it does not offer the same level of component isolation and testing capabilities.
react-native-testing-library
A library for testing React Native components. It focuses on providing utilities to test components in a way that resembles how they are used by end-users. While it complements @storybook/react-native by providing testing capabilities, it does not offer the interactive component development environment that Storybook provides.
Storybook for React Native
This readme is for v9 beta, for v8 docs see the v8.6 docs.
With Storybook for React Native you can design and develop individual React Native components without running your app.
If you are migrating from 8 to 9 you can find the migration guide here
For more information about storybook visit: storybook.js.org
Make sure you align your storybook dependencies to the same major version or you will see broken behaviour.

Table of contents
Getting Started
New project
There is some project boilerplate with @storybook/react-native
and @storybook/addon-react-native-web
both already configured with a simple example.
For expo you can use this template with the following command
npx create-expo-app --template expo-template-storybook AwesomeStorybook
For react native cli you can use this template
npx react-native init MyApp --template react-native-template-storybook
Existing project
Run init to setup your project with all the dependencies and configuration files:
npx storybook@latest init
The only thing left to do is return Storybook's UI in your app entry point (such as App.tsx
) like this:
export { default } from './.storybook';
Then wrap your metro config with the withStorybook function as seen below
If you want to be able to swap easily between storybook and your app, have a look at this blog post
If you want to add everything yourself check out the the manual guide here.
Additional steps: Update your metro config
We require the unstable_allowRequireContext transformer option to enable dynamic story imports based on the stories glob in main.ts
. We can also call the storybook generate function from the metro config to automatically generate the storybook.requires.ts
file when metro runs.
Expo
First create metro config file if you don't have it yet.
npx expo customize metro.config.js
Then wrap your config in the withStorybook function as seen below.
const path = require('path');
const { getDefaultConfig } = require('expo/metro-config');
const withStorybook = require('@storybook/react-native/metro/withStorybook');
const config = getDefaultConfig(__dirname);
module.exports = withStorybook(config, {
enabled: true,
configPath: path.resolve(__dirname, './.storybook'),
});
React native
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const path = require('path');
const withStorybook = require('@storybook/react-native/metro/withStorybook');
const defaultConfig = getDefaultConfig(__dirname);
const config = {};
const finalConfig = mergeConfig(defaultConfig, config);
module.exports = withStorybook(finalConfig, {
enabled: true,
configPath: path.resolve(__dirname, './.storybook'),
});
Reanimated setup
Make sure you have react-native-reanimated
in your project and the plugin setup in your babel config.
plugins: ['react-native-reanimated/plugin'],
Writing stories
In storybook we use a syntax called CSF that looks like this:
import type { Meta, StoryObj } from '@storybook/react';
import { MyButton } from './Button';
const meta = {
component: MyButton,
} satisfies Meta<typeof MyButton>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Basic: Story = {
args: {
text: 'Hello World',
color: 'purple',
},
};
You should configure the path to your story files in the main.ts
config file from the .storybook
folder.
import { StorybookConfig } from '@storybook/react-native';
const main: StorybookConfig = {
stories: ['../components/**/*.stories.?(ts|tsx|js|jsx)'],
addons: [],
};
export default main;
Decorators and Parameters
For stories you can add decorators and parameters on the default export or on a specifc story.
import type { Meta } from '@storybook/react';
import { Button } from './Button';
const meta = {
title: 'Button',
component: Button,
decorators: [
(Story) => (
<View style={{ alignItems: 'center', justifyContent: 'center', flex: 1 }}>
<Story />
</View>
),
],
parameters: {
backgrounds: {
values: [
{ name: 'red', value: '#f00' },
{ name: 'green', value: '#0f0' },
{ name: 'blue', value: '#00f' },
],
},
},
} satisfies Meta<typeof Button>;
export default meta;
For global decorators and parameters, you can add them to preview.tsx
inside your .storybook
folder.
import type { Preview } from '@storybook/react';
import { withBackgrounds } from '@storybook/addon-ondevice-backgrounds';
const preview: Preview = {
decorators: [
withBackgrounds,
(Story) => (
<View style={{ flex: 1, color: 'blue' }}>
<Story />
</View>
),
],
parameters: {
backgrounds: {
default: 'plain',
values: [
{ name: 'plain', value: 'white' },
{ name: 'warm', value: 'hotpink' },
{ name: 'cool', value: 'deepskyblue' },
],
},
},
};
export default preview;
Addons
The cli will install some basic addons for you such as controls and actions.
Ondevice addons are addons that can render with the device ui that you see on the phone.
Currently the addons available are:
Install each one you want to use and add them to the main.ts
addons list as follows:
import { StorybookConfig } from '@storybook/react-native';
const main: StorybookConfig = {
addons: [
'@storybook/addon-ondevice-notes',
'@storybook/addon-ondevice-controls',
'@storybook/addon-ondevice-backgrounds',
'@storybook/addon-ondevice-actions',
],
};
export default main;
Using the addons in your story
For details of each ondevice addon you can see the readme:
Hide/Show storybook
Storybook on react native is a normal React Native component that can be used or hidden anywhere in your RN application based on your own logic.
You can also create a separate app just for storybook that also works as a package for your visual components.
Some have opted to toggle the storybook component by using a custom option in the react native developer menu.
withStorybook wrapper
withStorybook
is a wrapper function to extend your Metro config for Storybook. It accepts your existing Metro config and an object of options for how Storybook should be started and configured.
const { getDefaultConfig } = require('expo/metro-config');
const withStorybook = require('@storybook/react-native/metro/withStorybook');
const defaultConfig = getDefaultConfig(__dirname);
module.exports = withStorybook(defaultConfig, {
enabled: true,
});
Options
enabled
Type: boolean
, default: true
Determines whether the options specified are applied to the Metro config. This can be useful for project setups that use Metro both with and without Storybook and need to conditionally apply the options. In this example, it is made conditional using an environment variable:
const { getDefaultConfig } = require('expo/metro-config');
const withStorybook = require('@storybook/react-native/metro/withStorybook');
const defaultConfig = getDefaultConfig(__dirname);
module.exports = withStorybook(defaultConfig, {
enabled: process.env.WITH_STORYBOOK,
});
onDisabledRemoveStorybook
Type: boolean
, default: false
If onDisabledRemoveStorybook true
and enabled
is false
, the storybook package will be removed from the build.
This is useful if you want to remove storybook from your production build.
useJs
Type: boolean
, default: false
Generates the .storybook/storybook.requires
file in JavaScript instead of TypeScript.
configPath
Type: string
, default: path.resolve(process.cwd(), './.storybook')
The location of your Storybook configuration directory, which includes main.ts
and other project-related files.
websockets
Type: { host: string?, port: number? }
, default: undefined
If specified, create a WebSocket server on startup. This allows you to sync up multiple devices to show the same story and arg values connected to the story in the UI.
websockets.host
Type: string
, default: 'localhost'
The host on which to run the WebSocket, if specified.
websockets.port
Type: number
, default: 7007
The port on which to run the WebSocket, if specified.
getStorybookUI options
You can pass these parameters to getStorybookUI call in your storybook entry point:
{
initialSelection?: string | Object (undefined)
-- initialize storybook with a specific story. eg: `mybutton--largebutton` or `{ kind: 'MyButton', name: 'LargeButton' }`
storage?: Object (undefined)
-- {getItem: (key: string) => Promise<string | null>;setItem: (key: string, value: string) => Promise<void>;}
-- Custom storage to be used instead of AsyncStorage
onDeviceUI?: boolean;
-- show the ondevice ui
enableWebsockets?: boolean;
-- enable websockets for the storybook ui
query?: string;
-- query params for the websocket connection
host?: string;
-- host for the websocket connection
port?: number;
-- port for the websocket connection
secured?: boolean;
-- use secured websockets
shouldPersistSelection?: boolean;
-- store the last selected story in the device's storage
theme: Partial<Theme>;
-- theme for the storybook ui
}
Using stories in unit tests
Storybook provides testing utilities that allow you to reuse your stories in external test environments, such as Jest. This way you can write unit tests easier and reuse the setup which is already done in Storybook, but in your unit tests. You can find more information about it in the portable stories section.
Contributing
We welcome contributions to Storybook!
- ๐ฅ Pull requests and ๐ Stars are always welcome.
- Read our contributing guide to get started,
or find us on Discord and look for the react-native channel.
Looking for a first issue to tackle?
- We tag issues with Good First Issue when we think they are well suited for people who are new to the codebase or OSS in general.
- Talk to us, we'll find something to suits your skills and learning interest.
Examples
Here are some example projects to help you get started