What's the best way to access the Rails.env in javascript?
Currently I am using ReactJS with existing Rails project, using webpacker gem. (Not using react-rails gem)
What I tried?
Set a javascript variable from the rails application (view template where my root component present) and access it in the root component JS. But this approach looks verbose to me as I needed to pass that variable all over the JS files in the react app.
Is there any better way of doing this?
You can use gon gem. Gon allow you to push rails variables to global variables in js.
In controller:
and then you can access variable in js this way
Another (in my opinion better) solution is query for data you need to another rails action. In your react component you can use
componentDidMountfor thisThen you can send data to all child component via props