1

I'd like to get in my RN app a variable from the environment somehow.

I've tried few things like: MY_VAR=foo node... this doesn't pass my JS file

I've tried using the --root option in RN CLI but this doesn't give me a complete solution as when using xcode to actually run the code with the bundler, they run, hard-coded index.js

What are my options?

I need to know somehow - where this code runs - which for it, to my understanding i need to pass some environment variable somehoe

YardenST
  • 5,119
  • 2
  • 33
  • 54
  • Possible duplicate https://stackoverflow.com/q/33117227/1985912 ? – Striped Jan 24 '18 at 12:07
  • This explains how environment variables work for React apps https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d – TimT May 20 '18 at 05:24

1 Answers1

0

It depends on the method you create your RN app, but here are the options for both:

if using react-native init: react-native-config, it's an easier set up than so far what i read. -setup .env.dev and a few others you want. yarn add react-native config, set up using gradle, IOS has another method so read through the documents. then run ENVFILE=.env.dev react-native run-ios

create-react-native-app: You may have the webpack, which means it would help you.

If you decided to eject your app from create-react-native-app, please go with react-native-config.

https://webpack.js.org/plugins/environment-plugin/

Good luck!

DaemonS
  • 91
  • 1
  • 8