admin管理员组文章数量:1401653
Is it possible to add custom developer options to your React Native app? For instance I would like to add the possibility to change the endpoint to which the app connects so I can switch between localhost, staging, production etc. on my mobile phone
Is it possible to add custom developer options to your React Native app? For instance I would like to add the possibility to change the endpoint to which the app connects so I can switch between localhost, staging, production etc. on my mobile phone
Share Improve this question asked Feb 28, 2017 at 6:53 MahoniMahoni 7,47618 gold badges60 silver badges119 bronze badges 1- how do you build it ? Webpack ? – zb' Commented Mar 5, 2017 at 7:18
5 Answers
Reset to default 3 +100With webpack you can use proccess.environment plugin, so you will be able to use
if (process.env.NODE_ENV === 'dev') {
makeYourThingIncludingRequereETC();
}
https://github./webpack/docs/wiki/list-of-plugins#environmentplugin
it will be transpiled to if ('prod' === 'dev') {}
in prod environment before build and minification, which will be removed from code due to 'always false' rule.
The best way to configure your react native app for different environments is to use
react-native-config
The best way to do this is to add some fields to a settings screen in your app with some switches and text fields to configure your developer settings. Then use React Native's __DEV__
variable to decide whether to show these extra fields.
iOS's own Developer section is an example of this:
The advantage of this approach is that you can more finely configure individual options in your app. This is what many of the top apps do internally, whether they're using React Native or not.
You could use the __DEV__
variable for this (https://facebook.github.io/react-native/docs/javascript-environment.
A small example:
So what you could do is to make a "DeveloperComponent".
...
class DeveloperComponent extends Component {
...
changeEnv(env) {
// change used urls/keys/other based on env
}
...
render() {
return (
<View>
<DeveleoperOption1 onPress={this.changeEnv("prod")} />
<DeveleoperOption2 onPress={this.changeEnv("dev")} />
<DeveleoperOption3 onPress={/* do other devlike action */} />
</View>
);
}
}
...
And add it to one of your upper ponents if you want it available everywhere, or in a settings page for instance. If added to an upper level, the "DeveloperComponent" could be a button, triggering an alert with the developer options.
...
return (
<View style={styles.container}>
<YourRouterContainer />
{__DEV__ && <DeveloperComponentTrigger />}
</View>
);
...
or
...
return (
<View style={styles.container}>
<SomeOtherSettings />
{__DEV__ && <DeveloperComponent />}
</View>
);
...
And you dont seem to have to worry about devponents bloating your build too much: "Using a Babel transform, we were able to remove code living behind DEV statements, effectively reducing bundle size, which improves JavaScript parse time" From https://code.facebook./posts/895897210527114/dive-into-react-native-performance/
You can also use webpack's define plugin: https://webpack.js/plugins/define-plugin/
If you have lots of configuration difference, configure 2 sets of webpack config, one for production, one for dev
new webpack.DefinePlugin({
PRODUCTION: true)}
And anywhere in your code you want to handle separation, just use a simple if statement
if (PRODUCTION) {
--do your thing }
else {
-- do your other thing}
本文标签: javascriptSet up custom developer options in React NativeStack Overflow
版权声明:本文标题:javascript - Set up custom developer options in React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744313365a2600139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论