React Native Creating & Calling Function From Same Class Example

Function also known as Method is a very important part of any Programming Language. Because using Functions developer can use break its code into Modules. Each module holds a different task and it reduce the code redundancy. So in this tutorial we would going to create a react native Android iOS app with 2 functions 1st is Function without Parameter, 2nd is Function with Parameter and we would Calling Function on Button onPress-onClick.

Contents in this project Creating & Calling Function From Same Class Tutorial :

1. Import StyleSheet, Text, View, Button, Alert components in your App.js file.

2. Create a function named as SampleFunction1() in your project’s class. This function is Function Without Argument type function. We are just printing a Alert message in this function.

3. Create SampleFunction2() in your class. This function is Function With Argument type function and receives a argument(Value) send from calling time.You can set any name inside the brackets it will automatically get the value inside the variable. We are also printing the received value using Alert in this function.

4. Create a Root View in render’s return block.

5. Create 1st Button in Root View and Call the SampleFunction1() on Button onPress.

The method to call the function is this.FunctionName.bind(this) ,Here bind tell us that this function belongs to our same class.

6. Create 2nd Button in Root View and call the SampleFunction2() on Button onPress , Now pass any value as argument just after the this. You can also send multiple arguments on Function calling time.

7. Create CSS Style class :

8. Complete source code for App.js File :

Screenshots in Android mobile device :

Screenshot in iOS mobile device :

Calling Function

Leave a Reply

Your email address will not be published.