
This is the first post in a series of setting up a react native UI library development foundation. Please refer to the link collections hereSeries: The Ultimate React Native UI Library starter repo for the full series overview.
In the first step of setting up the ultimate UI library boilerplate we go through making a new init, and installing Storybook.
If you've done this sort of thing before you'll probably want to jump to the bottom where I install a package which will load stories dynamically
Oh right, I'm not using expo... ;)
The other parts
Article | Link |
---|---|
setup react native & @storybook/react-native | You are here now! |
setup react from scratch together with react native web | Step 2: Setting up react with react native web |
setup @storybook/react + react native web to run as a parallel storybook | Step 3: Setting up storybook with react native web: Show your mobile components in a browser |
React Native Init
If you are completely new to react native please follow the steps on theofficial react native docs to get your environment setup.
Init
npx react-native init RNStorybook
This will set up a fresh new react native project for you.
Make sure that the installation was successful by running both the android and iOS builds of the project before going ahead. I always take things step by step, so when things bug out then I know exactly where it worked and where it broke.
so for the people who forgot etc. here's the commands for doing so.
cd ios && pod installcd .. yarn run ios
or
yarn run android
Behold the welcome screen!
Android | iOS |
---|---|
![]() | ![]() |
Installing Storybook
Using the automated setup as recommended by the docs here:Storybook quick-start guide
inside the root folder run the following command to initiate the installation:
npx -p @storybook/cli sb init
When prompted if you want to install the react-native server accept.
After that open up your code editor and we will do the final steps in rendering storybook out on the screen.
inside the folder./storybook
open up index.js and we will add in our app name. You can either add it manually or you can be lazy as me and import the app name fromapp.json
in the root folder. It has the benefit that if the app name changes you won't need to go in and change it manually.
Below is the result:
./storybook/index.js
import{AppRegistry}from'react-native';import{getStorybookUI,configure}from'@storybook/react-native';import{nameasappName}from'../app.json';import'./rn-addons';// import storiesconfigure(()=>{require('./stories');},module);// Refer to https://github.com/storybookjs/storybook/tree/master/app/react-native#start-command-parameters// To find allowed options for getStorybookUIconstStorybookUIRoot=getStorybookUI({});// If you are using React Native vanilla and after installation you don't see your app name here, write it manually.// If you use Expo you can safely remove this line.AppRegistry.registerComponent(appName,()=>StorybookUIRoot);exportdefaultStorybookUIRoot;
after we have added our app name to Storybook, inside of the root folder, we openindex.js
. This is the default entry point for our React Native app.
Inside of./index.js
comment everything out and add the following line:
export{default}from'./storybook'
This will render storybook as the first entry to your app, but later if you want Storybook to be rendered inside of a tab-view or another type of screen you'll just add storybook as any other component. More on that in the Storybook docs.
Now when you run the following command we can start up ourReact Native
development server on port7007
:
yarn run storybook
It will give you the following screen:
Hold your horses you might say "That menu-bar never stops loading!" and you'll be right. This web interface is trying to connect to a iOS or Android emulator.
so run an emulator and if you put the browser window and the device side by side it should look like:
If you play around with this you notice that you can control the view which is being shown in the emulator / simulator from the browser. Neat right! ⭐️ It's a nice feature which makes navigating your component library on a device very fast and easy.
Setting up dynamic story-loading
As your project grows you don't want to add stories into storybook manually as is the default. It's tedious and you'll spent time "debugging" why your component does not show up.
In comesreact-native-storybook-loader
I really like this project because after setting it up I don't have to worry about adding any new stories every again.
Setup
Install
yarn add -dev react-native-storybook-loader
Add script to package.json
{"scripts":{......"prestorybook":"rnstl"......}}
Add into Storybook configuration
Open up./storybook/index.js
and modify the entry where the stories are loaded from:
./storybook/index.js
import{AppRegistry}from'react-native'import{getStorybookUI,configure}from'@storybook/react-native'import{nameasappName}from'../app.json'import{loadStories}from'./storyLoader'import'./rn-addons'// Add React native storybook loader here!configure(()=>{loadStories()// <------------------},module)// Refer to https://github.com/storybookjs/storybook/tree/master/app/react-native#start-command-parameters// To find allowed options for getStorybookUIconstStorybookUIRoot=getStorybookUI({})// If you are using React Native vanilla and after installation you don't see your app name here, write it manually.// If you use Expo you can safely remove this line.AppRegistry.registerComponent(appName,()=>StorybookUIRoot)exportdefaultStorybookUIRoot
Configure story loader
The last step in setting upreact-native-storybook-loader
is configuring in which directory it should look for stories.
Open uppackage.json
again and add a config field:
"config":{"react-native-storybook-loader":{"searchDir":["./src/components"],"pattern":"**/*.stories.js","outputFile":"./storybook/storyLoader.js"}}
I wan't it to look inside of the./src/components
directory but you can set it up to look in a different folder, or even add more places it should look in by adding them into thesearchDir
array. If you change the other fields you'll need to change your other config to match accordingly.
Adding Test component
To test that this part of the setup works lets add a test component and check so everything works.
I'm making a new directory calledsrc
and inside of the directory I'm adding a folder calledcomponents
->./src/components
and in these files I'm adding two new files calledTestComponent.js
&TestComponent.stories.js
and let's code a test component and add a story for storybook.
./src/components/TestComponent.js
importReactfrom'react';import{View,Text}from'react-native';functionTestComponent(){return(<View><Text>HellofromReactNative</Text></View>);}exportdefaultTestComponent;
and a story for storybook
./src/components/TestComponent.stories.js
importReactfrom'react';import{storiesOf}from'@storybook/react-native';importTestComponentfrom'./TestComponent';storiesOf('Test Component',module).add('example',()=><TestComponent/>);
Note that while using react native we have to use thestoriesOf
api from Storybook.
Running everything together
Let's test it out on your device of choice!
yarn run iOS
This will first run thereact-native-storybook-loader
script. It will output a reference to all the files matching the pattern*.stories.js
inside./src/components
to./storybook/storyloader.js
and load them into Storybook. After that it's running storybook as normal.
Here's what you should see:
Hopefully you'll see the test component on the screen.
Success!
What did we accomplish:
- initiated a new react native project.
- installed
@storybook/react-native
. - installed
@storybook/react-native-server
. - installed & configured
react-native-storybook-loader
. - Added our first test component and story.
If you like this content please bookmark the init post of this serieshere and stay tuned for part 2!
You can find the finished repository for the whole series on Github:react-native-storybook-boilerplate
Consider giving it a star or raising an issue, PRs are most welcome!
Top comments(16)

- LocationLeiden, Netherlands
- WorkSoftware Engineer
- Joined
This article is really good! I was struggling a lot reading in other places but this one got me right where it should be in the code.

- Email
- LocationMalmö, Sweden
- EducationMsc. Electrical Engineering: Design of Processors and Integrated Systems.
- WorkFullstack Engineer
- Joined
Hi 👋 thank you! 🤓 Glad I could help ☕️

In this tutorial, we redirect our root level index.js to storybook's index.js. But by doing this, we can no longer run the app when we're developing. We can only run storybook.
Is there some way we can fork inside index.js to either run the app, or run storybook in the emulator? Or for that matter, run the emulator by bypassing index.js and looking directly at storybook/index.js?

- Email
- LocationMalmö, Sweden
- EducationMsc. Electrical Engineering: Design of Processors and Integrated Systems.
- WorkFullstack Engineer
- Joined
I would suggest doing something like this in your entry file:
const useStorybook = {set this in some config file} <Boolean>const App = () => useStorybook ? <Storybook /> : <Application />AppRegistry.registerComponent(appName, () => App);

- Email
- LocationChennai, India
- EducationBachelor Of Engineering
- WorkReact Native Developer at Disprz
- Joined
Should we have the storybook as a separate repo or inside the same project? Because all stories.js will also bundle in the React Native App right?

Same question here, were you able to find any way to resolve this?

- Email
- LocationMalmö, Sweden
- EducationMsc. Electrical Engineering: Design of Processors and Integrated Systems.
- WorkFullstack Engineer
- Joined
It depends, if you are using this approach and having it in the same repo as the app you are planning to ship, then I would suggest doing something like this in your entry file:
const useStorybook = {set this in some config file} <Boolean>const App = () => useStorybook ? <Storybook /> : <Application />AppRegistry.registerComponent(appName, () => App);
Originally my thinking was to ship the ui-library as an NPM package and then installed in the app so the UI and the App could be developed in parallell.

same question here?

- LocationPlanet Earth
- Joined
Nice article. Thank you! 🙌
P.S. If you are using Typescript, make sure you have thetsx
extension for your components and stories. ⚠️

- Email
- LocationMalmö, Sweden
- EducationMsc. Electrical Engineering: Design of Processors and Integrated Systems.
- WorkFullstack Engineer
- Joined
Yes! If you go through the other steps in this series you will have that up and running :)
For further actions, you may consider blocking this person and/orreporting abuse