Sitemap
Better Programming

Advice for programmers.

Using Environment Variables in ReactJS

3 min readNov 4, 2019

--

Press enter or click to view image in full size
Photo by Daniel Mirlea on Unsplash

If you’re using ReactJS and are accessing some API endpoint, you may have come across environment variables. In this tutorial, I will take you through how to use environment variables.

Assumption: You’re familiar with the Create React App and are using it to create your React application.

Why You Need Environment Variables

You need environment variables for two things:

  1. To store sensitive information. Things like API keys and passwords are highly sensitive and you shouldn’t push them to version control. After adding the variables to the .env file, add the .env file to .gitignore files to make sure they don’t get exposed (we will discuss this later in). GitHub will tell you if you have accidentally pushed sensitive information if you’re using it, but you shouldn’t rely on that.
  2. To customize variables based on your environment, such as whether it is in production, development, or staging, etc.
Press enter or click to view image in full size
GitGuardian coming to my rescue after accidentally exposing an API key

Create React App supports custom environment variables without the need to install any…

--

--

Perez Ogayo
Perez Ogayo

Written by Perez Ogayo

Masters Student at Carnegie Mellon University. Interested in NLP for African languages. Tweets at @a_ogayo

Responses (14)