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.

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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

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

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

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. 🙂


Related Posts

Leave a Comment

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