从react组件访问环境变量 [英] accessing environment variable from react component

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

问题描述

我有一些非敏感数据需要根据 staging production ,我相信访问类似 process.env.NODE_ENV 的内容将无法在react组件本身内工作,只能在服务器端文件中工作,因此需要以某种方式通过这是我的反应组件。

I have some non sensitive data that I need to set to different values based on what environment node runs in staging or production, I believe accessing something like process.env.NODE_ENV will not work within a react component itself, only in a server side files, hence need a way to somehow pass this down to my react component.

它只是显示页脚组件中是否有字符串Staging或Production。

It is simply to show if string "Staging" or "Production" inside the footer component.

推荐答案

考虑使用 DefinePlugin


定义自由变量。用于使用调试
日志记录或添加全局常量进行开发构建。

Define free variables. Useful for having development builds with debug logging or adding global constants.

示例:

new webpack.DefinePlugin({
    VERSION: JSON.stringify("5fa3b9"),
    BROWSER_SUPPORTS_HTML5: true,
    TWO: "1+1",
    "typeof window": JSON.stringify("object")
})


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

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