by Sai gowtham

How to define a use environment variables in React app

Environment variables help us to use different configurations in our react app for example we can use different API for development and different for production without changing the whole app every time or hiding the feature which is not ready for production yet.

Defining Environment Variables

To define environment variables, first we need to create a file .env file in the root folder of our react app.

Now, we can define environment variables inside that file using REACT_APP_.VARIABLE_NAME=VALUE.

.env
REACT_APP_.BASE_URL=https://google.com

Note: Don’t commit .env file to your public GitHub repository, if you stored secrets like API_KEY or passwords.

Accessing Environment Variables

In our react app components, we can access the environment variables by using process.env.REACT_APP_.BASE_URL.

App.js
import React from "react";

export default function App(){
   return (
      <div>
        <a href={process.REACT_APP_.BASE_URL}>My app</a>      </div>
   )
}

Inside the public/index.html file we can access environment variables like this.

index.html
<base href="%REACT_APP_BASE_URL%">

The environment variables are injected into the app at build time.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course