Explore the Template Project
Learning Objectives
After completing this unit, you’ll be able to:
- Navigate the project structure for Mobile SDK React Native apps.
- Configure your project for debugging or production.
- Find the Mobile SDK React Native sample app and open source libraries on GitHub.
Mobile SDK Templates for React Native
When you create an app with forcereact, the script customizes a template with the arguments that you provide. This customized template is the project for your new Mobile SDK app. For React Native, the template defines a native container app that provides direct access to the native operating system. This container provides the bridge between your JavaScript code and the Android or iOS runtime. In some cases, your development needs might require adjustments to this underlying container. Let’s take a quick look at what’s in that app.
iOS Native Container
The iOS native part of the template project is almost identical to that of a native iOS project. Its two classes take care of:
- Configuring Mobile SDK for startup
- Instantiating and displaying the root view
- Initiating and handling login and authentication
Open <output_directory>/ios/FirstReact.xcworkspace in Xcode. If you skim through the workspace, it’s easy to think that you’re looking at a native iOS project. For example, the AppDelegate class includes most of the boilerplate code of a native iOS app. The React-specific code is in the setupRootViewController
method of AppDelegate
.
This method creates an instance of RCTRootView
, a React Native class, and assigns that instance as the app’s root view. RCTRootView
acts as the bridge between native UI components and your app’s React Native components. Notice that you initialize this class with the location (jsCodeLocation
) of a bundle that contains your JavaScript code.
Android Native Container
Open your FirstReact/android project in Android Studio, then go to the Project view and look under FirstReact/app/app/src/main/java/com.mytrail.react. There, you can find two native Java classes––MainActivity
and MainApplication
. These classes are similar to their native-only Mobile SDK counterparts.
The MainApplication
and MainActivity
classes fulfill the same purposes as in Android native projects:
- Configuring Mobile SDK for startup
- Instantiating and displaying the startup activity
- Initiating and handling login and authentication
The primary differences between native and React Native templates for these classes are their base classes. In React Native apps, MainApplication
extends Application
, as usual, but also implements ReactApplication
. MainActivity
extends SalesforceReactActivity
, which, in turn, extends ReactActivity
. ReactApplication
and ReactActivity
come from the Facebook React Native library. Looking closely at the implementations, you find that most code paths ultimately lead to a React Native object. These objects include React-centric specializations of many Mobile SDK classes, such as SalesforceReactSDKManager
.
For example, the MainApplication
code creates a ReactNativeHost
object and uses it to pass Mobile SDK React Native packages to the React Native framework.
Digging deeper into the SalesforceReactSDKManager.getReactPackage()
code, you find that it returns a new ReactPackage
object. This object overrides the createNativeModules()
base method to return exactly the list of packages you’d expect:
The React Native runtime calls this method and other overrides to weave the magic that executes JavaScript methods as native Mobile SDK code.
Configuring Your Build
Looking for where you configure your code to run in debug or release mode? That mechanism is a little different on Android than on iOS. During development and debugging, Android Studio expects to load the JS files directly from the development server. For release mode, it loads the JS files from a bundle on the device. You can override the default build configuration in the project’s android/app/build.gradle file. Follow the embedded instructions in the big comment at the top of the file. For example:
In iOS, the switch between debug and release modes is handled for you automatically.
Mobile SDK Sample App for React Native
If you’re itching to do some further explorations on your own, check out the MobileSyncExplorerReactNative sample app in the SalesforceMobileSDK-Templates repo on GitHub. This sample demonstrates all the React Native language features you’ve just learned about, as well as some Mobile SDK offline features you may not yet know about:
- SmartStore––An offline caching mechanism for storing data securely on the device.
- Mobile Sync––An offline synchronization mechanism for easily merging offline data changes with the source Salesforce records when a device regains its connectivity.
To build the MobileSyncExplorerReactNative sample app on your development machine, use the forcereact createwithtemplate command. Here's an example.
To run the app, start up the React Native development server.
- In a Terminal window in macOS or the command prompt in Windows, change to your app’s root directory. In our case, that’s MyMobileSyncExplorer/.
- Run one of the following commands:
- In macOS: yarn start or npm start
- In Windows: yarn run-script start-windows or npm run-script start-windows
When the command line reports “To open developer menu press 'd'”, you can run the app from within your development environment.
- For iOS:
- In Xcode, open MyMobileSyncExplorer/ios/MyMobileSyncExplorer.xcworkspace.
- Click Run.
- For Android:
- From the Android Studio Welcome screen, or from the File | Open menu item, navigate to MyMobileSyncExplorer/android/.
- Click OK.
- Click Run.
Dig Deeper!
To examine how the native library project defines bridges between Mobile SDK and React Native, see:
- iOS: ios/SalesforceReact project in the SalesforceMobileSDK-ReactNative repo.
- Android: libs/SalesforceReact project in the SalesforceMobileSDK-Android repo.
- Resources
- GitHub repo: React Native template for Mobile SDK
- GitHub repo: MobileSyncExplorerReactNative
- GitHub repo: Mobile SDK React Native components for iOS
- GitHub repo: Mobile SDK React Native components for Android
- Using SmartStore to Securely Store Offline Data
- Using Mobile Sync to Access Salesforce Objects