Storybook is an exceptional tool that empowers us to visualize and manage components with remarkable ease in this article we’ll discuss how can we integrate Storybook into our React Native project.
Let’s create a new React Native project with the name rntemplate by using this command
npx react-native@latest init rntemplateAlso, let’s run the project on the iOs simulator, I've cleaned up the App.tsx code
Let’s install the packages required for the storybook using this command
npm i -D @storybook/react-native @storybook/core-common@6.5.16 @react-native-async-storage/async-storage react-native-safe-area-context react-domnpm i -D @storybook/addon-ondevice-controls @storybook/addon-ondevice-actionsafter this install the pods for iOs, by running the following command
cd ios; pod install; cd ..Storybook configuration
We need to create certain files/folders for the storybook let’s create using the below command
mkdir .storybook && cd .storybook && touch main.js preview.js Storybook.tsxIt will create a .storybook folder like this
Paste the below code in
.storybook/main.js
module.exports = {
stories: [
'../src/Components/**/*.stories.?(ts|tsx|js|jsx)'
],
addons: ['@storybook/addon-ondevice-controls', '@storybook/addon-ondevice-actions'],
};.storybook/preview.js
export const decorators = [];
export const parameters = {};.storybook/Storybook.tsx
import { getStorybookUI } from '@storybook/react-native';
import './storybook.requires';
const StorybookUIRoot = getStorybookUI({});
export default StorybookUIRoot;metro.config.js
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
resolver: {
resolverMainFields: ['sbmodern', 'react-native', 'browser', 'main'],
},
};
module.exports = mergeConfig(getDefaultConfig(__dirname), config);Also, we need to update our package.json file and add the following scripts
"prestart": "sb-rn-get-stories",
"storybook-watcher": "sb-rn-watcher"Let’s create an src(root) folder in our directory and move the App.tsx file inside it don’t forget to update the path of App.tsx inside the index.js file otherwise it will throw an error.
Also, let’s create a Components folder inside src and under the Components folder Let’s create a folder named Button which will contain three files
Button.stories.tsx
Button.styles.ts
Button.tsx
index.tsLet’s discuss each file and write the code inside it
Get Adnaan Azam’s stories in your inbox
Join Medium for free to get updates from this writer.
Button.tsx
This file contains our two variants of Button, Primary Button, and Outline Button
import React from 'react';
import {Button, Pressable, Text} from 'react-native';
import {styles} from './Button.styles';
export const PrimaryButton = (props: {title: string}) => {
const {title} = props;
return <Button title={title} />;
};
export const OutlineButton = (props: {title: string}) => {
const {title} = props;
return (
<Pressable style={styles.Oultline}>
<Text style={styles.Outline__Text}>{title}</Text>
</Pressable>
);
};Button.stories.tsx
Over here we write our stories of buttons as in this code we have two buttons
1. Primary Button
2. Outline Button
and grabbing from the ./Button file and we are exporting each button with const exports with a title Primary button / Outline button props
import React from 'react';
import {PrimaryButton as PB, OutlineButton as OB} from './Button';
export default {
title: 'components/Button',
};
export const PrimaryButton = (args: {title: string}) => <PB {...args} />;
PrimaryButton.args = {
title: 'Primary button',
};
export const OutlineButton = (args: {title: string}) => <OB {...args} />;
OutlineButton.args = {
title: 'Outline button',
};Button.styles.ts
This file contains all the styling of buttons.
import {StyleSheet} from 'react-native';
export const styles = StyleSheet.create({
Oultline: {
borderWidth: 1,
borderBlockColor: '#d3d3d3',
borderRadius: 4,
paddingVertical: 10,
justifyContent: 'center',
alignItems: 'center',
},
Outline__Text: {
color: '#000',
},
});index.ts
This file will just export all the buttons that we’ve declared in the Button.tsx file
export * from './Button';We’ve completed our button stories and the final step is to update the App.tsx file like this.
// import React from 'react';
// import {View, Text, StyleSheet} from 'react-native';
// export default App = () => {
// return (
// <View style={styles.container}>
// <Text>Visit finoyou at wwww.finoyou.in</Text>
// </View>
// );
// };
// const styles = StyleSheet.create({
// container: {
// flex: 1,
// justifyContent: 'center',
// alignItems: 'center',
// },
// });
export {default} from '../.storybook/Storybook';We need to comment out our App code to run the storybook, therefore I've added this line which is at the bottom of the above code.
Let’s generate our stories by running the script that we’ve added inside the package.json file
npm run prestartLet’s re-build our project and run, you will see our storybook will be running
Yeah, we’ve successfully integrated the storybook into our React Native project.
All code will be accessible from here
Follow me on
LinkedIn: https://www.linkedin.com/in/mohd-adnaan-azam-b38151174/
Twitter: https://twitter.com/adnaanazam
If you guys having difficulty in managing your finances I've made Finoyou a finance management app using React Native give it a try
Website: https://www.finoyou.in
App: https://play.google.com/store/apps/details?id=com.finoyou
Thanks :)
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.










