Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

This tutorial will help you to create common functions like helpers in react native and how to use them in react native screen.

Here you will get

  1. How to create functions in react native
  2. How to export those functions in react native
  3. How to import or use functions in react native

Let start today’s article how to create and use the function in react-native

Here, I am assuming you guys already have created a project with screens.

First, we have to create a functions file with multiple functions like this and also we will see how to export functions.

functions.js

export function functionOne() {
	return “1”;
}

export function functionTwo() {
	return “2”;
}

On the functions.js file, we have created two functions where they are returning values;

We are using the keyword export before the function keyword because we want to use this function in other files. If you want to use the function in the same file then you haven’t need export functionality.

Let see how we can use these common functions in react native components file.

Well if want use those functions then first we have to import function like on below example.

ReactNativeComponents.js

import React from 'react';
import {functionOne, functionTwo} from 'functions';
import { View, Text } from 'react-native';

class ReactNativeComponents extends React.Component {
    constructor(props){
        super(props);
    }

	componentDidMount() {
		this.callFunction();
	}

    callFunction(){
        //do stuff. 
        // foo==bar
        functionOne(data);
        functionTwo(data)
    }
   render() {
      return (
         <View>
             <Text>React Components</Text>
         </View>
      );
   }
}

export default ReactNativeComponents;

Thanks for reading…