React Native access strings.xml from inside javascript code

Viewed 3931

Is there a way to access the current values from android/app/src/main/res/values/strings.xml from inside Javascript code? I want to place different endpoint URLs for each build, but I'm failing even to detect the type of build inside react-native code without having to resort to the __DEV__ variable (which can be turned off from the dev menu)

2 Answers

If anyone still wondering how it could be done, I did a small package that allow export variables from strings.xml to be used on JS.

You can use the rn-config-reader to do that. Just install the package:

npm install rn-config-reader

Configure inside the strings.xml the strings you want to use in JS:

<resources>
    <string-array name="export_variables">
        <item>TEST_CONFIG_FIELD</item>
    </string-array>
    <string name="TEST_CONFIG_FIELD">ConfigValue</string>
</resources>

Inside your javascript, just use the variable:

import RNConfigReader from 'rn-config-reader';
console.log(RNConfigReader.TEST_CONFIG_FIELD);

https://github.com/higoramp/rn-config-reader

Related