Skip to main content

🛠️ 1. Setup

Requirements

To use Sherlo, ensure the following packages are installed:

Install Sherlo

To install Sherlo, run:

npm install -D @sherlo/react-native-storybook
Native Modules

Sherlo adds native modules. If you are not using Expo, run:

  1. cd ios to navigate to the ios folder.
  2. pod install to install the iOS dependencies.

Storybook Component

To enable Sherlo interaction with Storybook, update the Storybook component.

info

Storybook component is typically exported from .storybook config directory at your project root.

.storybook/index.js
import AsyncStorage from "@react-native-async-storage/async-storage";
import { getStorybook } from "@sherlo/react-native-storybook";
import { view } from "./storybook.requires";

const Storybook = view.getStorybookUI({
const Storybook = getStorybook(view, {
storage: {
getItem: AsyncStorage.getItem,
setItem: AsyncStorage.setItem,
},
});

export default Storybook;

Storybook Entry Point

To grant Sherlo access to your Storybook, update the Root component.

info

Root component could be App.jsx or app/_layout.jsx, depending on your project structure.


Determine your Storybook build approach:

Generates a build with Storybook integrated into your application.

  • Single Build: Creates one build containing both app and Storybook
  • Runtime Switching: Toggle between app and Storybook during development
  • Automatic Access: Storybook is automatically available for testing
Root component
import { registerStorybook } from "@sherlo/react-native-storybook";
import Storybook from "./.storybook"; // updated Storybook component
import App from "./App"; // main App component

// Automatically adds Storybook to your build
registerStorybook(() => <Storybook />);

export default function Root() {
return <App />;
}
Shared UI Providers

Wrap UI-related providers (e.g. ThemeProvider) around both App and Storybook to ensure correct rendering.

Example code
Root component
import { registerStorybook } from "@sherlo/react-native-storybook";
import Storybook from "./.storybook"; // updated Storybook component
import App from "./App"; // main App component
// ...

registerStorybook(() => (
<SharedProviders>
<Storybook />
</SharedProviders>
));

function SharedProviders({ children }) {
return (
<ThemeProvider>
<SafeAreaProvider>
{children}
</SafeAreaProvider>
</ThemeProvider>
);
}

export default function Root() {
return (
<SharedProviders>
<ThemeProvider>
<SafeAreaProvider>
<AuthProvider>
<App />
</AuthProvider>
<SafeAreaProvider>
<ThemeProvider>
</SharedProviders>
);
}

Access Storybook via Dev Menu or openStorybook().

It's not supported by Expo Go, as it depends on native modules.

Dev Menu - Available only in development builds

  1. Shake your device or press:
  • iOS: Cmd ⌘+Ctrl+Z
  • Android: Cmd ⌘+M
  1. Click Toggle Storybook to show or hide Storybook.
Toggling Storybook visibility via the Dev Menu

openStorybook() - Available in all builds

Add a button to open Storybook using the openStorybook() function.

import { openStorybook } from "@sherlo/react-native-storybook";
import { Button } from "react-native";

function OpenStorybookButton() {
return <Button title="Open Storybook" onPress={openStorybook} />
}

This is useful for internal distribution, as it can be accessed without running a development build.

Optimize the production build by excluding Sherlo and Storybook code.
Root component
import App from "./App"; // main App component

if (process.env.ENV !== "production") {
const registerStorybook = require("@sherlo/react-native-storybook").registerStorybook;
const Storybook = require("./.storybook").default;

registerStorybook(() => <Storybook />);
}

export default function Root() {
return <App />;
}