为什么我的React Native应用无法在Storybook和应用之间切换?

huangapple go评论71阅读模式
英文:

Why does my React Native app not switch between storybook and the app?

问题

我遵循了https://storybook.js.org/tutorials/intro-to-storybook/react-native/en/get-started/中的步骤,以便在运行特定CLI命令时在故事书和应用程序之间切换。

首先,我有一个名为app.config.js的文件,内容如下:

module.exports = {
  name: 'mobile-app',
  slug: 'mobile-app',
  version: '1.0.0',
  extra: {
    storybookEnabled: process.env.STORYBOOK_ENABLED,
  },
  orientation: 'portrait',
  icon: './assets/icon.png',
  userInterfaceStyle: 'light',
  splash: {
    image: './assets/splash.png',
    resizeMode: 'contain',
    backgroundColor: '#ffffff',
  },
  assetBundlePatterns: ['**/*'],
  ios: {
    supportsTablet: true,
  },
  android: {
    adaptiveIcon: {
      foregroundImage: './assets/adaptive-icon.png',
      backgroundColor: '#ffffff',
    },
  },
  web: {
    favicon: './assets/favicon.png',
  },
};

然后,我更新了我的App.tsx文件,以根据STORY_ENABLED标志设置为true时在故事书和应用程序之间切换逻辑:

import { StatusBar } from 'expo-status-bar';
import { StyleSheet, View } from 'react-native';
import Constants from 'expo-constants';
import { ThemeProvider } from '@rneui/themed';
import theme from './styles/theme';
import { Text } from './components/Text/Text';

function App() {
  const styles = StyleSheet.create({
    container: {
      alignItems: 'center',
      backgroundColor: theme.lightColors?.gold120,
      flex: 1,
      justifyContent: 'center',
    },
  });

  return (
    <ThemeProvider theme={theme}>
      <View style={styles.container}>
        <Text h3>Whereas recognition of the inherent dignity</Text>
        <StatusBar style="auto" />
      </View>
    </ThemeProvider>
  );
}
let AppEntryPoint = App;

// 如果storybookEnabled为true,则渲染Storybook
if (Constants.expoConfig?.extra?.storybookEnabled === 'true') {
  /* eslint-disable-next-line @typescript-eslint/no-var-requires */
  AppEntryPoint = require('./.storybook').default;
}

export default AppEntryPoint;

请注意,我不得不添加了可选链和typescript的eslint禁用。

第三,我在package.json中添加了以下脚本:

 "scripts": {
// ...其他脚本,
    "storybook": "sb-rn-get-stories STORYBOOK_ENABLED='true' && expo start"
  },

与文档不同之处在于,在STORYBOOK_ENABLED='true'之前删除了&&,因为我使用的是Windows机器,根据https://stackoverflow.com/questions/75942395/react-native-storybook-problem-with-process-env-storybook-enabled 的建议。

我的想法是可以使用yarn start启动应用程序,使用yarn storybook将标志设置为true并启动storybook。

然而,yarn storybook总是打开应用程序,而不是storybook。我是否漏掉了什么?

英文:

I followed https://storybook.js.org/tutorials/intro-to-storybook/react-native/en/get-started/ in order to switch between storybook and the app when running certain CLI commands.

Firstly, I have an app.config.js file that looks like this:

module.exports = {
  name: &#39;mobile-app&#39;,
  slug: &#39;mobile-app&#39;,
  version: &#39;1.0.0&#39;,
  extra: {
    storybookEnabled: process.env.STORYBOOK_ENABLED,
  },
  orientation: &#39;portrait&#39;,
  icon: &#39;./assets/icon.png&#39;,
  userInterfaceStyle: &#39;light&#39;,
  splash: {
    image: &#39;./assets/splash.png&#39;,
    resizeMode: &#39;contain&#39;,
    backgroundColor: &#39;#ffffff&#39;,
  },
  assetBundlePatterns: [&#39;**/*&#39;],
  ios: {
    supportsTablet: true,
  },
  android: {
    adaptiveIcon: {
      foregroundImage: &#39;./assets/adaptive-icon.png&#39;,
      backgroundColor: &#39;#ffffff&#39;,
    },
  },
  web: {
    favicon: &#39;./assets/favicon.png&#39;,
  },
};

Afterwards, I updated my App.tsx file with logic to switch between storybook and the app if the STORY_ENABLED flag is set to true:

import { StatusBar } from &#39;expo-status-bar&#39;;
import { StyleSheet, View } from &#39;react-native&#39;;
import Constants from &#39;expo-constants&#39;;
import { ThemeProvider } from &#39;@rneui/themed&#39;;
import theme from &#39;./styles/theme&#39;;
import { Text } from &#39;./components/Text/Text&#39;;

function App() {
  const styles = StyleSheet.create({
    container: {
      alignItems: &#39;center&#39;,
      backgroundColor: theme.lightColors?.gold120,
      flex: 1,
      justifyContent: &#39;center&#39;,
    },
  });

  return (
    &lt;ThemeProvider theme={theme}&gt;
      &lt;View style={styles.container}&gt;
        &lt;Text h3&gt;Whereas recognition of the inherent dignity&lt;/Text&gt;
        &lt;StatusBar style=&quot;auto&quot; /&gt;
      &lt;/View&gt;
    &lt;/ThemeProvider&gt;
  );
}
let AppEntryPoint = App;

// Render Storybook if storybookEnabled is true
if (Constants.expoConfig?.extra?.storybookEnabled === &#39;true&#39;) {
  /* eslint-disable-next-line @typescript-eslint/no-var-requires */
  AppEntryPoint = require(&#39;./.storybook&#39;).default;
}

export default AppEntryPoint;

Note that I had to add the optional chaining and just an eslint disable for typescript.

Thirdly, I added the following script to my package.json:

 &quot;scripts&quot;: {
// ...scripts,
    &quot;storybook&quot;: &quot;sb-rn-get-stories STORYBOOK_ENABLED=&#39;true&#39; &amp;&amp; expo start&quot;
  },

The big difference from the documentation is removing the &amp;&amp; before STORYBOOK_ENABLED=&#39;true&#39; because I'm on a windows machine, according to https://stackoverflow.com/questions/75942395/react-native-storybook-problem-with-process-env-storybook-enabled

The idea is that I can use yarn start to start the app proper and use yarn storybook to set the flag to true and start storybook.

However, yarn storybook always opens the app instead. Am I missing something here?

答案1

得分: 1

教程模板最近已更新,以解决 Windows 的问题。

您可以重新下载模板或手动更新脚本以使用 cross-env。

"storybook": "sb-rn-get-stories && cross-env STORYBOOK_ENABLED='true' expo start",
"storybook:ios": "sb-rn-get-stories && cross-env STORYBOOK_ENABLED='true' expo start --ios",
"storybook:android": "sb-rn-get-stories && cross-env STORYBOOK_ENABLED='true' expo start --android"

还请确保将 cross-env 添加为开发依赖项。

yarn add -D cross-env

这应该解决您的问题。

英文:

The tutorial template got updated recently to add a fix for windows.

You can redownload the template or update the scripts manually to use cross-env.

&quot;storybook&quot;: &quot;sb-rn-get-stories &amp;&amp; cross-env STORYBOOK_ENABLED=&#39;true&#39; expo start&quot;,
&quot;storybook:ios&quot;: &quot;sb-rn-get-stories &amp;&amp; cross-env STORYBOOK_ENABLED=&#39;true&#39; expo start --ios&quot;,
&quot;storybook:android&quot;: &quot;sb-rn-get-stories &amp;&amp; cross-env STORYBOOK_ENABLED=&#39;true&#39; expo start --android&quot;

also make sure to add cross-env as a dev dependency

yarn add -D cross-env

That should solve your issue.

huangapple
  • 本文由 发表于 2023年6月22日 02:49:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定