React Native Expo 环境变量 [英] React Native Expo Environment Variables

查看:27
本文介绍了React Native Expo 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对本文和其他文章中解释的环境变量的概念感到满意https://www.freecodecamp.org/news/how-to-gracefully-use-environment-variables-in-a-react-native-app/

So I'm happy with the concept of environment variables as explained in this article and others https://www.freecodecamp.org/news/how-to-gracefully-use-environment-variables-in-a-react-native-app/

太好了,我的 SOMETHING="something" 已存储,因此我可以使用 env.SOMETHING 或其他任何东西

Great, I've got my SOMETHING="something" stored so I can just use env.SOMETHING or whatever

我有点迷失的部分是你保存实时变量的地方?

我宁愿不做这样的解决方案,因为您似乎仍然保持您的密钥相当公开,并且您只是根据 if 语句的环境进行选择

I would rather not do a solution like this as it seems you are still keeping your keys quite public and that you are just choosing based on the environment with if statements

使用 expo react native 管理环境

例如,对于我们拥有的 Express App 部署,我们指定

For example with an Express App deployment we have, we specify

let endPointURL = env.endPointURL

然后我们在本地保存该变量的版本,当它与 AWS 一起使用时,它会被 AWS 服务器覆盖,如 这里

and then we keep a versoin of that variable locally and when it sits with AWS it is overridden by AWS servers as explained here

我只是想知道 Android 和 iOS 版本(在各自的商店中)或通过 Expo 是否存在类似的东西?

I was just wondering does something like that exist for Android and iOS builds (on the respective stores) or through Expo?

谢谢大家

推荐答案

老实说,我认为他们的做法有点愚蠢.可能有比这更好的方法,但我认为我遵循了他们的文档建议.

Honestly I think the way they go about it is a little silly. There may be a better way to go about than this, but I think I followed their documentation suggestions.

https://docs.expo.io/versions/latest/distribution/release-channels/#using-release-channels-for-environment-variable-configuration

他们有一个代码片段建议您创建一个函数来查看发布配置本身.

They have a code snippet suggesting you create a function to look at the release configuration itself.

我解释说您可能会执行以下代码之类的操作,并将您的环境变量存储在 variables.js 文件中,然后像这样拉入您的环境变量.

I interpreted it that you might do something like the code below and store your environment variables in a variables.js file and pull in your environment variables as such.

import Constants from 'expo-constants';

export const prodUrl = "https://someapp.herokuapp.com";

const ENV = {
  dev: {
    apiUrl: "http://localhost:3000"
  },
  staging: {
    apiUrl: prodUrl
  },
  prod: {
    apiUrl: prodUrl
  }
};

function getEnvVars(env = "") {
  if (env === null || env === undefined || env === "") return ENV.dev;
  if (env.indexOf("dev") !== -1) return ENV.dev;
  if (env.indexOf("staging") !== -1) return ENV.staging;
  if (env.indexOf("prod") !== -1) return ENV.prod;
}

export default getEnvVars(Constants.manifest.releaseChannel);

现在 Expo 支持配置文件为 app.config.jsapp.config.ts,我们可以使用 dotenv.检查这个:https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file

Now that Expo supports config file as app.config.js or app.config.ts, we can use the dotenv. Check this: https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file

这篇关于React Native Expo 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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