Example to Call Functions of Other Class From Current Class in React Native

Introduction

This is an Example to Call Functions of Other Class From Current Class in React Native. This can be usually seen in object-oriented languages like Java; Where we make an object of the other class and call the function of it. In the case of React Native, you can also do the same by making an object of a class. You can also pass the arguments if you want.

Example is for Class Component only, Official document suggested to use Function Components after RN0.63

To call the function from other class

new OtherClass().functionWithoutArg();

OR

new OtherClass().functionWithArg('args');

In this example of Calling Functions of Other Class From Current Class, we will make an OtherClass and will define two functions with and without arguments and after that, we will call these functions from Our default class.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code

App.js

/*This is an Example of Calling Other Class Function in React Native*/
import React, { Component } from 'react';
//import React in our project
import { StyleSheet, View, Alert, Platform, Button } from 'react-native';
//import all the components we will need in our project

export default class App extends Component {
  handlerSimpleCall = () => {
    //Calling a function of other class (without arguments)
    new OtherClass().functionWithoutArg();
  };

  handlerArgCall = () => {
    //Calling a function of other class (with argument)
    new OtherClass().functionWithArg('About React');
  };

  render() {
    return (
      <View
        style={{
          justifyContent: 'center',
          alignItems: 'center',
          flex: 1,
          backgroundColor: '#F5FCFF',
        }}>
        <View style={{ margin: 10 }}>
          <Button
            title="Function Without Argument"
            onPress={this.handlerSimpleCall}
            color="#606070"
          />
        </View>
        <View style={{ margin: 10 }}>
          <Button
            title=" Function With Argument"
            onPress={this.handlerArgCall}
            color="#606070"
          />
        </View>
      </View>
    );
  }
}

class OtherClass extends Component {
  functionWithoutArg = () => {
    //function to be called from default class (without args)
    alert('Function Called Without Argument ');
  };

  functionWithArg = Value => {
    //function to called from default class (with args)
    alert(Value);
  };
}

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshot

          

Output in Online Emulator

This is how you can call the function of another class from the current class. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.