反应本机Expo环境变量 [英] React Native Expo Environment Variables

查看:321
本文介绍了反应本机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

,然后我们在本地保留该变量的versoin,当它在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环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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