2

I saw that there is the __DEV__ variable, and I would like to add my own, say __USE_THIS__. Then way I set this is when the Xcode project is building, but I don't know how to pass that to the js part. Can I use the bundle script and pass it there somehow? When is the __DEV__ set, and how?

4
  • you want ot pass a variable value to the js file? Commented Feb 23, 2017 at 18:43
  • 1
    thi looks somehow related: github.com/luggit/react-native-config Commented Feb 23, 2017 at 19:55
  • I want to pass something like __USE_THIS__ = true, and in that case the js will do something different than if it was false (by default). Commented Feb 23, 2017 at 22:51
  • I checked the react-native-config, but that uses .env, and that means I will pass those everywhere. I would be happy with a simpler way of just passing something one-way from xcode to js. Commented Feb 23, 2017 at 22:53

1 Answer 1

5

You could have Target/Scheme specific variables exposed to javascript by writing them to a file during Build Phase and then import them in the javascript.

1) Add the following to "Bundle React Native code and images" under "Build Phase" for your target:

export NODE_BINARY=node
echo '{"target": "MyTarget"}' > ../build-info.json
../node_modules/react-native/packager/react-native-xcode.sh

Example screenshot from xcode

2) Import them in the javascript, for example in index.ios.js

import buildInfo from '../../build-info.json'
console.log(buildInfo.target)

It should also be possible to write a file to $PROJECT_ROOT/.env and use it through something like react-native-config instead if you prefer

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.