DEV Community

Danny
Danny

Posted on

How to use React Native Web with Storybook v7 beta

You may have heard that Storybook is getting close to a v7 release. If you're using addon-react-native-web you might be wondering how to try out the beta. In this quick guide I will show you how to setup a new project and add @storybook/addon-react-native-web Storybook to the project.
This enables you to use the web Storybook with your react native components.

If you already have this setup in an existing project it should be pretty straight forward to get updated. Change the version of @storybook/addon-react-native-web to next and update all your other relevant storybook dependencies to the latest v7 beta version (you can use next here too). Then you should check this migration guide to see if you need to update anything to get your project ready for v7.

To setup a fresh new project you can follow this guide:

First I'll generate a project.

yarn create expo-app
Enter fullscreen mode Exit fullscreen mode

Next I'll open up the project and in the root of the project run

npx sb@next init --type react
Enter fullscreen mode Exit fullscreen mode

After its done add @storybook/addon-react-native-web@next and the dependencies for it.

yarn add --dev react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web@next
Enter fullscreen mode Exit fullscreen mode

Add the addon to .storybook/main.js as follows:

const config = {
  /* existing config */
  addons: [/*existing addons,*/ "@storybook/addon-react-native-web"],
  /* more config here */
};
export default config;
Enter fullscreen mode Exit fullscreen mode

If you're familiar with the Storybook config you'll notice there are some slight differences in the main.js file. One cool new feature is that you can now get benefits of autocomplete even if you aren't using typescript for your config files. This change comes from the comment above the config object that looks like this

/** @type { import('@storybook/react-webpack5').StorybookConfig } */
const config = {
Enter fullscreen mode Exit fullscreen mode

Now that the setup is done, let's add a React Native component and a story in the stories folder.

You may want to delete the ReactJS example components. To do that delete everything inside of the stories folder.

rm -rf stories/*
Enter fullscreen mode Exit fullscreen mode

Then in that folder you can add this example button component.

Inside stories/ create Button.jsx

// stories/Button.jsx
import React from "react";
import { StyleSheet, Text, TouchableOpacity, View } from "react-native";

const styles = StyleSheet.create({
  button: {
    paddingVertical: 8,
    paddingHorizontal: 16,
    borderRadius: 4,
    alignSelf: "flex-start",
    flexGrow: 0,
    backgroundColor: "purple",
  },
  buttonText: {
    color: "white",
    fontSize: 16,
    fontWeight: "bold",
  },
  buttonContainer: {
    alignItems: "flex-start",
    flex: 1,
  },
});

export const Button = ({ text, onPress, color, textColor }) => (
  <View style={styles.buttonContainer}>
    <TouchableOpacity
      style={[styles.button, !!color && { backgroundColor: color }]}
      onPress={onPress}
      activeOpacity={0.8}
    >
      <Text style={[styles.buttonText, !!textColor && { color: textColor }]}>
        {text}
      </Text>
    </TouchableOpacity>
  </View>
);
Enter fullscreen mode Exit fullscreen mode

Now add the files Button.stories.jsx in the stories folder with this content:

// stories/Button.stories.jsx
import { Button } from "./Button";

export default {
  component: Button,
};

export const Basic = {
  args: {
    text: "Hello World",
    color: "purple",
  }
}
Enter fullscreen mode Exit fullscreen mode

One of the really cool updates in V7 is the introduction of CSF3 which you can see above. There is now much less boilerplate needed to write a story!

At this point all thats left to do is run yarn storybook and get to work building your components!

button in the storybook ui

Thanks for reading.

Follow me on twitter @Danny_H_W

Top comments (11)

Collapse
 
spotnick profile image
spotnick

Thanks for the tutorial! Do you have an idea why it isnt working with TypeScript? I receive the following error:

Image description

Thanks :)

Collapse
 
dannyhw profile image
Danny

Hey, this does work with typescript. That looks like a bundling problem, what version of storybook are you using?

Collapse
 
spotnick profile image
spotnick

Thanks for your help @dannyhw. I am using Storybook 7.0.9 and did everything exactly like you mentioned it in your tutorial. I just tried the same with just .js components to eliminate if it was an error related to typescript but I get the same above message with .js stories and components. Do you have any ideas? :)

Thread Thread
 
dannyhw profile image
Danny

You might be missing some babel config. In v7 storybook uses the projects babel configuration.

I’ll run through this later today and see if this needs updating.

Thread Thread
 
spotnick profile image
spotnick

Hm that sounds valid. My current babel config is pretty simple..

module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['nativewind/babel'],
};
};

can you maybe post yours? :)

Thread Thread
 
spotnick profile image
spotnick

Ok after following this guide storybook.js.org/docs/react/config... to configure babel for storybook 7 its working now! Thank you for your help! :)

Collapse
 
blwinters profile image
Ben Winters

This looks great. In my case, it wasn't so simple because I don't use Expo, but I was able to get the v7 @storybook/addon-react-native-web working by configuring webpack within the Storybook main.ts file. I've shared a more detailed explanation and the code here.

Collapse
 
dannyhw profile image
Danny • Edited

Hey Ben thanks for sharing.

A few things I wanted to point out.

  1. Using expo or not shouldn't be a factor since the webpack config does not require expo.
  2. aliases for things like ../../Utilities/Platform and other react native files isn't necessary assuming you have modules-to-transpile setup with all the packages that ship untranspiled.
Collapse
 
cflorez10 profile image
Cristian Florez • Edited

Thanks for this tutorial, Im trying to follow the tutorial for an existing project... Im getting the following error when I run the command yarn storybook:

Image description

This is my main.js configuration:

Image description

and my SB dependencies:

Image description

Collapse
 
choneyse profile image
Chris Honeysett

I was running into the exact same issue when upgrading my project to use Storybook v7. When I commented out both addon-ondevice-controls and addon-ondevice-actions everything started working.

module.exports = {
  stories: ['../components/**/*.stories.?(ts|tsx|js|jsx)'],
  addons: [
    '@storybook/addon-react-native-web',
    // '@storybook/addon-ondevice-controls',
    // '@storybook/addon-ondevice-actions',
  ],
  framework: {
    name: '@storybook/react-webpack5',
    options: {},
  },
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
dannyhw profile image
Danny • Edited

Those addons cannot be used together, you should have separate config folders for react native storybook and react-native-web storybook.

In the starter projects I do this by having a .ondevice folder for the react native config.

Also for v7 you need to use 7.6.10-rc.0 which was just released today.

If you want to try it I recommend using the template project like this

For expo:
npx create-expo-app --template expo-template-storybook@next AwesomeStorybook

For rncli:
npx react-native init AwesomeStorybook --template react-native-template-storybook@next