Y
Y
Yunique2018-08-28 04:55:57
Ruby on Rails
Yunique, 2018-08-28 04:55:57

How to use .env files in react?

The project uses the dotenv
gem which allows you to create an .env file with environment variables. How can these variables be used in react? Those. how to call a specific variable in react?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-08-28
@Yunique33

1. Read, parse .env and pass variables to code using webpack.DefinePlugin .
2. Don't waste time and use a ready-made library like dotenv-webpack .
3. Using Rails, write variables to the template:

<script>
  window.env = {
    ENV_VARIABLE: '...',
  };
</script>

L
lnked, 2018-08-28
@lnked

I implemented it like this
1) when building or starting the project, I read the necessary .env file (depending on the flag with which the launch is going)
https://github.com/lnked/react-starter/blob/master...
2) then through DefinePlugin, I run it in process.env
https://github.com/lnked/react-starter/blob/master...
3) I already use the received data in react scripts
https://github.com/lnked/react-starter/ blob/master...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question