React Native – How to define app resource?

Create a new file .env in the root of your React Native app

API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh

Then access variables defined there from your app

How to share global constant inside the app

Create a “global.js” file

import { Platform, YellowBox } from 'react-native'
import Config from 'react-native-config'

global.p = (...args) => {
  console.log(...args)
}

// API calls will not work while this is uncommented, it is just for debuggin and populating the network tab
// global.XMLHttpRequest = global.originalXMLHttpRequest ? global.originalXMLHttpRequest : global.XMLHttpRequest

global.PLATFORM = Platform.OS
global.IS_ANDROID = Platform.OS === 'android'
global.IS_IOS = Platform.OS === 'ios'
global.IS_IPAD = Platform.OS === 'ios' && Platform.isPad

global.BASE_URI = Config.BASE_URI

global.API_MOCK = Config.API_MOCK === 1 || Config.API_MOCK === '1'

global.CONFIG_URI = Config.CONFIG_URI || ''
global.SETTINGS_URI = Config.SETTINGS_URI
global.SETTINGS_KEY = Config.SETTINGS_KEY

YellowBox.ignoreWarnings([
  'Warning: isMounted(...) is deprecated',
  'Module RCTImageLoader',
  'Module RNGoogleSignin',
  'Setting a timer'
])

Be the first to comment

Leave a Reply

Your email address will not be published.


*