Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global helper function in react native?

Tags:

react-native

How can I create Global Helper functions in react-native?

I would like to use for accessing sqlite database or fetching data from server. Can I create a JavaScript file with functions and call that functions from multiple views?

like image 875
Phyo Avatar asked Nov 05 '15 08:11

Phyo


1 Answers

global variable

class MainActivity extends Component {

  constructor(){
     super();

     // Creating Global Variable.
     global.SampleVar = 'This is Global Variable.';
  }
}

in second activity

class SecondActivity extends Component {

  render(){
    return(
       <View>
          <Text> {global.SampleVar} </Text>
       </View>
   );
  }
}

But if you want to have a global function

export function TestFunc1() {

}

export function TestFunc2() {

}

Then import and use

import { TestFunc1 } from './path_to_file'
like image 164
Mahdi Bashirpour Avatar answered Sep 21 '22 17:09

Mahdi Bashirpour