惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
B
Blog
博客园_首页
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
量子位
V
V2EX
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
I
InfoQ
博客园 - 【当耐特】

Nx Blog

Sharing Tailwind CSS Styles Across Apps in a Monorepo | Nx Blog How SiriusXM Stays Competitive by Iterating and Getting to Market Fast | Nx Blog Agentic Experience Is the New Developer Experience | Nx Blog Nx Joins the Linux Foundation and the Agentic AI Foundation | Nx Blog A Monorepo Is NOT a Monolith | Nx Blog Why we deleted (most of) our MCP tools | Nx Blog Teach Your AI Agent How to Work in a Monorepo | Nx Blog How Broadcom stays efficient and nimble with monorepos | Nx Blog Why Monorepos are King in the Age of AI | Nx Blog Nx 2026 Roadmap: Expanding Agent Autonomy, Improving Performance, Better Polyglot and More | Nx Blog End to End Autonomous AI Agent Workflows with Nx | Nx Blog Autonomous Agents at Scale | Nx Blog Scaling 700+ Projects: How Nx Became a 'No-Brainer' for Caseware | Nx Blog Configure Tailwind v4 with Angular in an Nx Monorepo | Nx Blog The Missing Multiplier for AI Agent Productivity | Nx Blog A Year of Nx Webinars | Nx Blog Wrapping Up 2025 | Nx Blog Nx 22.3 Release: Angular 21 Support, tsgo Compiler, and Prettier v3 | Nx Blog Nx Cloud Release: Agent Resource Usage | Nx Blog Nx Platform Outperforms DIY Cache by 5x | Nx Blog An Nx Carol: Past, Present, and Future of Your Monorepo | Nx Blog Nx 22.1 Release: Terminal UI on Windows, Storybook 10, Vitest 4, and more! | Nx Blog The Compounding Effect: How Nx Features Multiply Performance Gains | Nx Blog 10 Monorepo Myths Debunked: Separating Fact from Fiction | Nx Blog Nx Cloud Release: Enterprise Task Analytics | Nx Blog Watch and Rebuild Storybook Dependencies with Nx | Nx Blog Book - React for Enterprise: Timeless Architecture for Enterprise Apps | Nx Blog Beyond Remote Cache: Unlock 70% More CI Performance | Nx Blog Nx 22 Release: Expanding the build platform | Nx Blog What's the Point of Generating All This Code If You Can't Merge It? | Nx Blog
Use Storybook with Nx React Native | Nx Blog
Emily Xiong · 2022-04-26 · via Nx Blog

In my previous blogs (see links at the end), I wrote about how to develop Nx React Native applications. However, as developers, we are constantly searching for ways to make the developer experience better.

This blog will show how to add Storybook to Nx React Native applications. With Nx, you don't need to go through this long guideline to set up the Storybook, you can quickly get it running.

Example Repo: xiongemi/studio-ghibli-search-engine

Storybook:

Storybook View (left: Android, right: iOS)

Setup

First, you need to add @nrwl/storybook to your existing Nx React Native workspace:

# npm
npm install @nrwl/storybook --save-dev

# yarn
yarn add --dev @nrwl/storybook

Then you need to generate the storybook configuration for your app or lib:

nx g @nrwl/react-native:storybook-configuration **<your app or lib>**

As shown in the example below, 3 folders got generated:

  • .storybook at workspace root
  • .storybook in your app or lib
  • storybook in your app (Note: this folder is for creating the Storybook UI component. It will only be created for the app, you will not see this for lib.)

If you choose to automatically generate *.stories file, you should see the default story looks like below:

import { storiesOf } from '@storybook/react-native';
import React from 'react';

import { Loading } from './loading';

const props = {};

storiesOf('Loading', module).add('Primary', () => <Loading {...props} />);

To gather the stories you created, run the command:

nx storybook **<your app or lib>**

You should see in the terminal saying:

Writing to <your workspace>/.storybook/story-loader.js

In your <your workspace>/.storybook/story-loader.js, it should list your stories created under your app or lib similar to the below example:

// Auto-generated file created by react-native-storybook-loader
// Do not edit.
//
// https://github.com/elderfo/react-native-storybook-loader.git

function loadStories() {
  require('../apps/studio-ghibli-search-engine-mobile/src/app/App.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/film/film.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/results/film-list-item/film-list-item.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/results/people-list-item/people-list-item.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/results/result-list-item/result-list-item.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/search/search.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/shared/film-card/film-card.stories');
  require('../apps/studio-ghibli-search-engine-mobile/src/app/shared/loading/loading.stories');
}

const stories = [
  '../apps/studio-ghibli-search-engine-mobile/src/app/App.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/film/film.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/results/film-list-item/film-list-item.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/results/people-list-item/people-list-item.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/results/result-list-item/result-list-item.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/search/search.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/shared/film-card/film-card.stories',
  '../apps/studio-ghibli-search-engine-mobile/src/app/shared/loading/loading.stories',
];

module.exports = {
  loadStories,
  stories,
};

Also, notice that in your app's main file, the import of the App changed to storybook/toggle-storybook:

import App from './storybook/toggle-storybook';

View Storybook for App

To view the storybook on the simulator/emulator/device, start the app like you usually do:

# iOS
nx run-ios <your app>

# Android
nx run-android <your app>

In your simulator/emulator/device, open the Debug Menu by entering d in terminal. You should see the menu option Toggle Storybook in the Debug Menu:

Screenshot of Debug menu (left: Android, right: iOS)

When switching on the toggle, you should see the list of your component stories:

Storybook View (left: Android, right: iOS)

View Storybook for Lib

Note: the storybook can only be viewed inside an app. To view the storybook for lib in the workspace, you need to first set up the storybook for an app in the workspace.

Then run the command:

nx storybook **<your lib>**

This should update the .storybook/story-loader.js with stories in your lib.

Then just run the command to start your app, you should see the storybook for your lib.

Troubleshooting

Error: Couldn't find a navigation object

If you are using the library @react-navigation/native and you are using hooks like useNavigtion and useRoute inside your component, you are likely to get the below error:

Render Error for Couldn't find a navigation object

The easiest way is just to mock this library and create a decorator for it:

src/storybook/mocks/navigation.tsx

import { NavigationContainer } from '@react-navigation/native';
import React from 'react';

export const NavigationDecorator = (story) => {
  return (
    <NavigationContainer independent={true}>{story()}</NavigationContainer>
  );
};

Mock Navigation Decorator

Then in your story, you just need to add the above NavigationDecorator:

import { storiesOf } from '@storybook/react-native';
import { mockFilmEntity } from '@studio-ghibli-search-engine/models';
import React from 'react';

import { NavigationDecorator } from '../../../storybook/mocks/navigation';

import FilmListItem from './film-list-item';

storiesOf('FilmListItem', module)
  .addDecorator(NavigationDecorator)
  .add('Primary', () => <FilmListItem film={mockFilmEntity} />);

Add NavigationDecoration to the story

Now, this error should go away and you should see your component in your storybook.

If your component is using the useRoute hook and expecting certain routing parameters, then you need to customize the mock NavigationDecorator for your component. For example, below is a component that is expecting an id from the route parameters:

const route = useRoute<RouteProp<{ params: { id: string } }>>();
const id = route.params?.id;

The mock NavigationDecorator will become:

import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import React from 'react';

const NavigationDecorator = (story) => {
  const Stack = createNativeStackNavigator();
  return (
    <NavigationContainer independent={true}>
      <Stack.Navigator>
        <Stack.Screen
          name="MyStorybookScreen"
          component={story}
          initialParams={{ id: 123 }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

Error: Could not find "store"

If you are using Redux store and your component is stateful and connected to the store, you are likely to get the below error:

Render Error for Could not find "store"

The simple solution is to mock the store. First, you need to install the library redux-mock-store and its typing:

# npm
npm install redux-mock-store @types/redux-mock-store --save-dev# yarn
yarn add redux-mock-store @types/redux-mock-store --dev

Similarly, like how you mock up the navigation, you need to mock up the store. The below example mocks the store with the initial root state:

src/storybook/mocks/store.tsx

import {
  initialRootState,
  RootState,
} from '@studio-ghibli-search-engine/store';
import React from 'react';
import { Provider as StoreProvider } from 'react-redux';
import configureStore from 'redux-mock-store';

export const StoreDecorator = (story) => {
  const mockStore = configureStore<RootState>([]);
  const store = mockStore(initialRootState);
  return <StoreProvider store={store}>{story()}</StoreProvider>;
};

You can add this store decorator to your story:

people-list-item.stories.tsx

import { storiesOf } from '@storybook/react-native';
import { mockPeopleEntity } from '@studio-ghibli-search-engine/models';
import React from 'react';

import { NavigationDecorator, StoreDecorator } from '../../../storybook/mocks';

import PeopleListItem from './people-list-item';

storiesOf('PeopleListItem', module)
  .addDecorator(StoreDecorator)
  .addDecorator(NavigationDecorator)
  .add('Primary', () => <PeopleListItem people={mockPeopleEntity} />);

Error: Actions must be plain objects

If you use an async action (for example, an action created using createAsyncThunk from @reduxjs/toolkit), you would likely run into the below error: Actions must be plain objects.

Render Error for Actions must be plain objects

Now to resolve this, add thunk to mock store middleware:

import {
  initialRootState,
  RootState,
} from '@studio-ghibli-search-engine/store';
import React from 'react';
import { Provider as StoreProvider } from 'react-redux';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';

export const StoreDecorator = (story) => {
  const mockStore = configureStore<RootState>([thunk]);
  const store = mockStore({ ...initialRootState });
  return <StoreProvider store={store}>{story()}</StoreProvider>;
};

Conclusion

Here are how to use Storybook with Nx React Native and some common errors you may run into. With Nx React Native, you can quickly view Storybook with a toggle option in Debug Menu. It allows developers to interact and test with components during development.

Where to go from here?