英文:
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: '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',
},
};
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 '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;
// Render Storybook if storybookEnabled is true
if (Constants.expoConfig?.extra?.storybookEnabled === 'true') {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
AppEntryPoint = require('./.storybook').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
:
"scripts": {
// ...scripts,
"storybook": "sb-rn-get-stories STORYBOOK_ENABLED='true' && expo start"
},
The big difference from the documentation is removing the &&
before STORYBOOK_ENABLED='true'
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.
"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"
also make sure to add cross-env as a dev dependency
yarn add -D cross-env
That should solve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论