selcukctn
selcukctn

Reputation: 255

How to use global variable in react native?

I want to get a string from the globalvari.js file in my codes and print it on the screen. How can I do?

import {a} from '../../globalvari'

export default class App extends Component{

 render() {

  var fav = ["item", "kopla"];

   return (
     <View style={styles.MainContainer}>

         { fav.map((item, key)=>(
         <Text key={key} style={styles.TextStyle}> { item } </Text>)
         )}

         { a.map((item, key)=>(
         <Text key={key} style={styles.TextStyle}> { item } </Text>)
         )}


     </View>
   );
 }
}

globalvari.js:

var a = ["harrik","hsc"];

Upvotes: 2

Views: 2757

Answers (1)

Oscar Sales DEV
Oscar Sales DEV

Reputation: 476

You need to "export" anything you want to use in another file:

var a = ["harrick", "hsc"];
export a;

var b = 324;
export b;

Then:

import { a, b } from '.... ';

Upvotes: 3

Related Questions