Reputation: 25
I want to store APIs in configuration file so when I deployed this on Development or on Production I just have to change the url on config file not in all js file. But I don't know how to use configuration file in react.js
I tried using react-global-configuration but didn't got any results ! Can anyone please tell me how to achieve this ?
Upvotes: 2
Views: 887
Reputation: 1426
I suggest you use the npm package dotenv.
You create a .env
file in the root of your repository, and this package helps your project access any variable that's inside your .env
file. You just need to:
npm install dotenv
Then you add this line to your App.js
require('dotenv').config()
and you're good to go!
Example:
> cat .env
DB_HOST="https://myserver.herokuapp.com"
To access the variable's name, you just have to call process.env.DB_HOST
in your code.
NOTE:
If your project has been created using create-react-app
, then you must name the variables like this:
REACT_APP_DB_HOST="https://myserver.herokuapp.com"
and if you want to access it you use:
process.env.REACT_APP_DB_HOST
Upvotes: 4