首页 > 移动平台 > 详细

[React] Use Environment Variables with Create React App

时间:2020-06-02 09:14:16      阅读:48      评论:0      收藏:0      [点我收藏+]

Environment variables are a standard way to configure variables in your app based on the current environment (development, test, production). This lesson explores how to use environment variables specifically with Create React App, starting with the built in NODE_ENV variable, as well as how to create custom environment variables for your app.

 

 .env: 
REACT_APP_CUSTOM=<custom_value_in_dotenv_file>

 

It must start with "REACT_APP_", otherwise, it doesn‘t work;

 

Inside component, we can do:

    <div className="App">
      <pre>process.env.NODE_ENV: {process.env.NODE_ENV}</pre>
      <pre>process.env.REACT_APP_CUSTOM: {process.env.REACT_APP_CUSTOM}</pre>
    </div>

 

It is recommended to push .env file to git, so can share with team.

 

And always you can overwrite with .env.local file

REACT_APP_CUSTOM=<custom_value_in_dotenv_file>

 

[React] Use Environment Variables with Create React App

原文:https://www.cnblogs.com/Answer1215/p/13028874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!