We need .env files to save data that can be changed even when the project is deployed on server. If we save the constants in a constant.js file than this file will also be deployed on the server. And If we want to make some chnages in that we need to deployed the code of the project again.
When we call apis in React JS or any other Framework we need Base Url and endpoint of the Api like
If we are running our project on our local system. The Base url will be http://localhost:3000 and endpoint for login api will be something like /api/login.
But if we deploy the project on server its endpoint remains same but its Base url is changed.
So to manage the situation we make constant in .env file and use it in the whole project code. And deploy the project on server. Once project is deployed we have option to change the Base url of the project using environment variables concept.
Example :
Note - We can install package .dotenv to use any name of the variable.
If we don't want to install the package then we can prefix our env variables with REACT_APP
then it will also work in React app without installing any packages.
Check the video for more detailed explaination :
Please leave a comment if you have any questions . Will be happy to help you.
All THE BEST Guys.
Comments
Post a Comment