To use .env in react :
1.You need to create .env file .
2.Open the .env file and declare your environment variable.
VITE_REACT_APP_NAME=react app
VITE_REACT_APP_CLIENT_ID=adkksclsc.cscd52
Note: React enforces the prefix REACT_APP on every custom variable. Variable without REACT_APP is ignored. So always create a custom variable like REACT_APP_CUSTOM_VARIABLE.
But if you are using vite you need to add the prefix vite .
3.Now to print your environment variable you need to put process.env before your custom variable.
{process.env.REACT_APP_NAME}
4.Now start your application npm start and refresh the browser to see the changes.
Top comments (0)