Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Step 1: Setting up React Native with Storybook
Carl-W
Carl-W

Posted on • Edited on

     

Step 1: Setting up React Native with Storybook

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

ArticleLink
setup react native & @storybook/react-nativeYou are here now!
setup react from scratch together with react native webStep 2: Setting up react with react native web
setup @storybook/react + react native web to run as a parallel storybookStep 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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

or

yarn run android
Enter fullscreen modeExit fullscreen mode

Behold the welcome screen!

AndroidiOS
Alt TextAlt Text

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
Enter fullscreen modeExit fullscreen mode

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;
Enter fullscreen modeExit fullscreen mode

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'
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

It will give you the following screen:

Alt Text

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:

Alt Text

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
Enter fullscreen modeExit fullscreen mode

Add script to package.json

{"scripts":{......"prestorybook":"rnstl"......}}
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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"}}
Enter fullscreen modeExit fullscreen mode

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;
Enter fullscreen modeExit fullscreen mode

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/>);
Enter fullscreen modeExit fullscreen mode

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:

successful-test

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 & configuredreact-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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
mateoguzmana profile image
Mateo Guzmán
I do a lot of things nowadays, I don't even know what's my title anymore. Full-stack dev (?)
  • Location
    Leiden, Netherlands
  • Work
    Software 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.

CollapseExpand
 
ugglr profile image
Carl-W
Fullstack developer | Front to back in Typescript
  • Email
  • Location
    Malmö, Sweden
  • Education
    Msc. Electrical Engineering: Design of Processors and Integrated Systems.
  • Work
    Fullstack Engineer
  • Joined

Hi 👋 thank you! 🤓 Glad I could help ☕️

CollapseExpand
 
marysue profile image
Mary Lark
  • Joined

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?

CollapseExpand
 
ugglr profile image
Carl-W
Fullstack developer | Front to back in Typescript
  • Email
  • Location
    Malmö, Sweden
  • Education
    Msc. Electrical Engineering: Design of Processors and Integrated Systems.
  • Work
    Fullstack 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);
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
davidzabaleta profile image
David Zabaleta
  • Joined

No one of the options works for me, the first option loads the emulator but the menu-bar keeps loading and the 2nd neither works :'(
I need to add something to storyloader.js?

CollapseExpand
 
dom_vinyard_c1e05eb0212f profile image
Dom Vinyard
  • Joined

same, using expo

CollapseExpand
 
ashwin_mothilal profile image
Ashwin Mothilal
Combination of love towards gadgets, new technologies, developing Mobile Apps.
  • Email
  • Location
    Chennai, India
  • Education
    Bachelor Of Engineering
  • Work
    React 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?

CollapseExpand
 
thenriquedb profile image
Thiago Henrique Domingues
  • Location
    Brasil
  • Work
    Software developer at Montreal
  • Joined

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

CollapseExpand
 
ugglr profile image
Carl-W
Fullstack developer | Front to back in Typescript
  • Email
  • Location
    Malmö, Sweden
  • Education
    Msc. Electrical Engineering: Design of Processors and Integrated Systems.
  • Work
    Fullstack Engineer
  • Joined
• Edited on• Edited

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);
Enter fullscreen modeExit fullscreen mode

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.

CollapseExpand
 
chasty profile image
Willy Rosa Huanca
  • Location
    Cusco, Peru
  • Work
    Developer at Paytoperu S.A.C
  • Joined

same question here?

CollapseExpand
 
vladimirvovk profile image
Vladimir Vovk
Heya! 👋 I am passionate about Mobile and Web technologies, React Native, React, building beautiful user experiences, and making the world a better place.
  • Location
    Planet Earth
  • Joined
• Edited on• Edited

Nice article. Thank you! 🙌

P.S. If you are using Typescript, make sure you have thetsx extension for your components and stories. ⚠️

CollapseExpand
 
esackn profile image
EsackN
  • Joined

Hi, Good explanation. I want to preview the storybook on web without emulator. If this is possible please help me to achieve this?

CollapseExpand
 
ugglr profile image
Carl-W
Fullstack developer | Front to back in Typescript
  • Email
  • Location
    Malmö, Sweden
  • Education
    Msc. Electrical Engineering: Design of Processors and Integrated Systems.
  • Work
    Fullstack Engineer
  • Joined

Yes! If you go through the other steps in this series you will have that up and running :)

CollapseExpand
 
marysue profile image
Mary Lark
  • Joined

Despite putting the config block in package.json, I still had to manually run the prestorybook target in scripts first before running ios so that the storybook/storyLoader.js file was created.

CollapseExpand
 
xiongemi profile image
Emily Xiong
A frontend developer in Toronto
  • Location
    Toronto, Canada
  • Joined

the command should be *yarn add -D react-native-storybook-loader *

CollapseExpand
 
deepakanandrao profile image
Deepak Anandrao
  • Location
    Canada
  • Work
    Developer at Doodle
  • Joined

or
yarn add --dev react-native-storybook-loader

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Fullstack developer | Front to back in Typescript
  • Location
    Malmö, Sweden
  • Education
    Msc. Electrical Engineering: Design of Processors and Integrated Systems.
  • Work
    Fullstack Engineer
  • Joined

More fromCarl-W

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp