react-native-view-controller v0.5.0
react-native-view-controller
Base UIViewController
classes on iOS and base Fragment
/ Activity
on Android. View Controllers on both platforms are providing a straight forward API to create a base controller via dependency injection of the module name as well as initial props for rendering the root component represented from the module name. This allows to not have to subclass a base controller for every component that will be rendered. If dependency injection is not possible, the base controller classes also providing hooks for subclassing to provide great flexibility.
DISCLAIMER
This project is currently in beta.
Core APIs are subject to change. We encourage people to try this library out and provide us feedback as we get it to a stable state.
Getting started
$ npm install react-native-view-controller --save
Mostly automatic installation
$ react-native link react-native-view-controller
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-view-controller
and addMSRReactNativeViewController.xcodeproj
- In XCode, in the project navigator, select your project. Add
libMSRReactNativeViewController.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import net.mischneider.MSRReactNativeViewControllerPackage;
to the imports at the top of the file - Add
new MSRReactNativeViewControllerPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-view-controller' project(':react-native-view-controller').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-view-controller/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-view-controller')
Usage
On iOS the base view controller is represented by the MSRReactNativeViewController
:
// Create a MSRReactNativeViewController and push or present it on the current stack. It uses a MSRReactNativeBridgeManager as view provider.
MSRReactNativeBridgeManager *bridgeManager = [MSRReactNativeBridgeManager sharedManager];
NSString *moduleName = ...; // Name of the root component that should be rendered
NSDictionary *initialProperties = ...; // Some initial props that should be passed as initial props to the componentn
MSRReactNativeViewController *reactNativeViewController = [[MSRReactNativeViewController alloc] initWithModuleName:moduleName initialProperties:initialProperties viewProvider:bridgeManager];
// Push or present the React Native view controller on the current navigation stack
[self.navigationController pushViewController:reactNativeViewController animated:YES];
On Android the base view controller is represented by the ReactNativeActivity
and the ReactNativeFragment
classes:
// Push a React Native Activity on the current stack.
// Create an intent to for the React Native activity that should be presented. `this` is the current activity.
Intent intent = new Intent(this, ReactNativeActivity.class);
// Add the module name you would like to launch within the activity
String moduleName = ...; // Module name of the root component that should be rendered
intent.putExtra(ReactNativeActivity.MainComponentNameIntentName, moduleName);
// Add the launch options that will get passed down as initial props to the component represented by the module name
Bundle options = ...; // Bundle that is passed as initial props
intent.putExtra(ReactNativeActivity.LaunchOptionsIntentName, options);
// Start the ReactNativeActivity
startActivity(intent);
License
Copyright 2018 Michael Schneider
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.