如何在React Native应用程序中设置.env? [英] How can we set up .env in react native application?

查看:557
本文介绍了如何在React Native应用程序中设置.env?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在React Native应用程序中设置.env?如何在react-native内部进行环境设置.

How can we set up .env in react native application? How to do environment setup inside react-native.

推荐答案

我使用"react-native-dotenv"包.安装它,然后将"react-native-dotenv"预设添加到项目根目录下的.babelrc文件中,如下所示:

I use 'react-native-dotenv' package. Install it and add "react-native-dotenv" preset to your .babelrc file at the project root, like this:

{
  "presets": ["react-native", "react-native-dotenv"]
}

然后创建一个.env文件并在其中添加属性,就像这样:

Then create a .env file and add your properties there, may be like this:

YOUR_FIELD="value"

最后,您应该可以在src文件中使用它,如下所示:

finally, you should be able to use it in your src files, like this:

import { YOUR_FIELD } from 'react-native-dotenv';
console.log(YOUR_FIELD) // prints "value"

如果在使用此软件包时遇到问题,则可以在其GitHub页面上找到进一步的说明,就在这里: https://github.com/zetachang/react-native-dotenv

if you run into an issue while using this package, then you can look for further clarification in their GitHub page, right here: https://github.com/zetachang/react-native-dotenv

希望这会有所帮助.

这篇关于如何在React Native应用程序中设置.env?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆