Problem
I am creating an app with React Native and firebase. I am using two tabs, and I want to pass the key from inside one tab to another tab (using tabNavigator
/reactNavigation
for the tabs). I don't know how to define a global variable. I did some reaserch and figured out that I needed to use some sort of .global
mand. I've tried using:
var passKey = '';
global.passKey = key; //defined before any screens
and when that didn't work:
this.global.setState({ passKey: item.key }) //defined in button in a flatlist
The statement this.global.setState({})
gives me an error, and global.passKey
might save the variable globally, but when I change the variable inside of the screens, one variable doesn't pass over to the other screen. I would really love some help getting a solution to passing a variable between two tabs. Thank you!
Problem
I am creating an app with React Native and firebase. I am using two tabs, and I want to pass the key from inside one tab to another tab (using tabNavigator
/reactNavigation
for the tabs). I don't know how to define a global variable. I did some reaserch and figured out that I needed to use some sort of .global
mand. I've tried using:
var passKey = '';
global.passKey = key; //defined before any screens
and when that didn't work:
this.global.setState({ passKey: item.key }) //defined in button in a flatlist
The statement this.global.setState({})
gives me an error, and global.passKey
might save the variable globally, but when I change the variable inside of the screens, one variable doesn't pass over to the other screen. I would really love some help getting a solution to passing a variable between two tabs. Thank you!
3 Answers
Reset to default 3It works this way:
global.js
export default {};
Usage
import global from './global';
global.passKey = 123;
Although you made your choice of accepted answer, I believe this is what you need.
Well, if it's just a static variable that doesn't change over time, create a constants.js
file somewhere in your project and do export const MY_VARIABLE = 'theValue'
. After that you can import it into the file which needs it - import { MY_VARIABLE } from './constants'
You have to use redux, if you want to get your variable in two tabs. But if you dont use it before it can be a little plicated when you first start check the docs here https://redux.js/