Step 1: Setting up React Native with Storybook
Tags: react, tutorial, javascript, beginners
Published: 2020-08-02 03:18:18

---
title: Step 1: Setting up React Native with Storybook
published: true
description: In the first step of setting up the ultimate UI library boilerplate we go through making a new init, and installing Storybook.
tags: react, tutorial, javascript, beginners
cover_image: https://appinventiv.com/wp-content/uploads/sites/1/2019/01/appDevelopment-reactNative-reduceCost.jpg
---

> This is the first post in a series of setting up a react native UI library development foundation. Please refer to the link collections here [Series: The Ultimate React Native UI Library starter repo](https://dev.to/ugglr/series-the-ultimate-react-native-ui-library-starter-repo-bho) 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](https://dev.to/ugglr/step-2-setting-up-react-with-react-native-web-30ba) |
| 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](https://dev.to/ugglr/step-3-setting-up-storybook-with-react-native-web-show-your-mobile-components-the-browser-12ke) |

## React Native Init

If you are completely new to react native please follow the steps on the [official react native docs](https://reactnative.dev/docs/environment-setup) 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 install

cd .. 

yarn run ios
```
or
```
yarn run android
```

Behold the welcome screen!

| Android | iOS |
| ------- | --- |
| ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lps57g4urh9yskl8dilo.png) | ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1s4pm42llok7up3t3l10.png) |  


## Installing Storybook

Using the automated setup as recommended by the docs here: [Storybook quick-start guide](https://storybook.js.org/docs/guides/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 from `app.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`
```js
import {AppRegistry} from 'react-native';
import {getStorybookUI, configure} from '@storybook/react-native';
import {name as appName} from '../app.json';

import './rn-addons';

// import stories
configure(() => {
  require('./stories');
}, module);

// Refer to https://github.com/storybookjs/storybook/tree/master/app/react-native#start-command-parameters
// To find allowed options for getStorybookUI
const StorybookUIRoot = 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);

export default StorybookUIRoot;
```

after we have added our app name to Storybook, inside of the root folder, we open `index.js`. This is the default entry point for our React Native app. 

Inside of `./index.js` comment everything out and add the following line:

```js
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 our `React Native` development server on port `7007`:

```
yarn run storybook
```

It will give you the following screen:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9255g2w1ytqw48fqajry.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/pie6baj8gpqu3wmgok39.gif)

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 comes [react-native-storybook-loader](https://github.com/elderfo/react-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**
```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`
```js
import { AppRegistry } from 'react-native'
import { getStorybookUI, configure } from '@storybook/react-native'
import { name as appName } 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 getStorybookUI
const StorybookUIRoot = 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)

export default StorybookUIRoot
```
**Configure story loader**

The last step in setting up `react-native-storybook-loader` is configuring in which directory it should look for stories.

Open up `package.json` again and add a config field:

```json
"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 the `searchDir` 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 called `src` and inside of the directory I'm adding a folder called `components` -> `./src/components`

and in these files I'm adding two new files called `TestComponent.js` & `TestComponent.stories.js` and let's code a test component and add a story for storybook. 

`./src/components/TestComponent.js`
```js
import React from 'react';
import {View, Text} from 'react-native';

function TestComponent() {
  return (
    <View>
      <Text>Hello from React Native</Text>
    </View>
  );
}

export default TestComponent;
```

and a story for storybook

`./src/components/TestComponent.stories.js`
```js
import React from 'react';
import {storiesOf} from '@storybook/react-native';

import TestComponent from './TestComponent';

storiesOf('Test Component', module).add('example', () => <TestComponent />);
```

Note that while using react native we have to use the `storiesOf` api from Storybook. 

## Running everything together

Let's test it out on your device of choice!

`yarn run iOS`

This will first run the `react-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](https://dev-to-uploads.s3.amazonaws.com/i/joecw62ah9dkfoop16zj.png)

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 series [here](https://dev.to/ugglr/series-the-ultimate-react-native-ui-library-starter-repo-bho) and stay tuned for part 2!

You can find the finished repository for the whole series on Github: [react-native-storybook-boilerplate](https://github.com/ugglr/react-native-storybook-boilerplate)

Consider giving it a star or raising an issue, PRs are most welcome!