Call External Local JS File Activity in Current App.js File in React Native

React Native supports the module calling structure which is used to divide the project code into multiple segments and each class has its unique JS file. We would write the code into different files and import each file into our App.js file according to our requirement. So in this tutorial we would going to create a react native app and import load Call External Local JS File as individual Activity in Current App.js File in React Native.

What we are doing in this project – Must Read :

In this tutorial we would going to add 2 activities in our project and each activity has its own JS file named as MainActivity.js and SecondActivity.js file. Now we would import both files into our App.js file and using the StackNavigator we would call both files into App.js file. So let’s get started .

Contents in this project Call External Local JS File Activity in Current App.js File in React Native Android iOS app :

1. Add
reactnavigation  library in your project because without the react-navigation library we cannot use the
StackNavigator in our project and without the 
StackNavigator we cannot add multiple activities in our project. So open your project folder in CMD( Command Prompt / Terminal ). like i did in below screenshot and type the
npm install save reactnavigation  command inside it and press enter to install.


2. The final screenshot of command prompt after done installing the react-navigation library.

3. Create a folder in your application project folder. For example my project name is Myproject. So i am going to create a new folder named as Modules in it. This folder is used to Store the both activity files.

4. Create a New JS file named as MainActivity.js inside the Modules folder. This would be our MainActivity of project. You can learn more about the Stack Navigator about here on my main activity tutorial.

static navigationOptions :  Used to set the Activity tile which would show inside the application Action Title bar.

NavigateActivityFunction() : This function is used to open the Second Activity on button click. We would call this function on button onPress event.

export default MainActivity : This line of code is very important because its tell the application that this code would be load default in this current JS file.

5. Create another JS file named as SecondActivity.js file inside the Modules folder. This would be our Second Activity.

6. Screenshot of Modules folder after crating both files.

7. Open the App.js file of your project and import the Component inside it.

8. Now we would import the both MainActivity and SecondActivity files from Modules folder.

9. Import StackNavigator in your App.js file.

10. Now finally we would call the both Activity files using StackNavigator in our App.js file.

11. Complete source code for App.js File :

Screenshot in Android device :

Call External Local JS File Activity Screenshot in iOS device :