react-native/docs
Christian Brevik f426a83d1b Add props for overriding native component
Summary:
Opening a new PR for #10946 (see discussion there).

This PR builds upon #14775 (iOS ViewManager inheritance) and #14261 (more extensible Android WebView).

**Motivation**
When `WebView.android.js` and `WebView.ios.js` use `requireNativeComponent`, they are hard-coded to require `RCTWebView`. This means if you want to re-use the same JS-logic, but require a custom native WebView-implementation, you have to duplicate the entire JS-code files.

The same is true if you want to pass through any custom events or props, which you want to set on the custom native `WebView`.

What I'm trying to solve with this PR is to able to extend native WebView logic, and being able to re-use and extend existing WebView JS-logic.

This is done by adding a new `nativeConfig` prop on WebView. I've also moved the  extra `requireNativeComponent` config to `WebView.extraNativeComponentConfig` for easier re-use.

**Test plan**
jacobp100 has been kind enough to help me with docs for this new feature. So that is part of the PR and can be read for some information.

I've also created an example app which demonstrates how to use this functionality: https://github.com/cbrevik/webview-native-config-example

If you've implemented the native side as in the example repo above, it should be fairly easy to use from JavaScript like this:
```javascript
import React, { Component, PropTypes } from 'react';
import { WebView, requireNativeComponent, NativeModules } from 'react-native';
const { CustomWebViewManager } = NativeModules;

export default class CustomWebView extends Component {
  static propTypes = {
    ...WebView.propTypes,
    finalUrl: PropTypes.string,
    onNavigationCompleted: PropTypes.func,
  };

  _onNavigationCompleted = (event) => {
    const { onNavigationCompleted } = this.props;
    onNavigationCompleted && onNavigationCompleted(event);
  }

  render() {
    return (
      <WebView
        {...this.props}
        nativeConfig={{
          component: RCTCustomWebView,
          props: {
            finalUrl: this.props.finalUrl,
            onNavigationCompleted: this._onNavigationCompleted,
          },
          viewManager: CustomWebViewManager
        }}
      />
    );
  }
}

const RCTCustomWebView = requireNativeComponent(
  'RCTCustomWebView',
  CustomWebView,
  WebView.extraNativeComponentConfig
);
```

As you see, you require the custom native implementation at the bottom, and send in that along with any custom props with the `nativeConfig` prop on the `WebView`. You also send in the `viewManager` since iOS requires that for `startLoadWithResult`.

**Discussion**
As noted in the original PR, this could in principle be done with more React Native components, to make it easier for the community to re-use and extend native components.
Closes https://github.com/facebook/react-native/pull/15016

Differential Revision: D5701280

Pulled By: hramos

fbshipit-source-id: 6c3702654339b037ee81d190c623b8857550e972
2017-09-19 16:01:02 -07:00
..
Accessibility.md New guide: Improving User Experience 2017-08-24 14:01:53 -07:00
AndroidBuildingFromSource.md Document Android native communication 2017-09-19 14:16:41 -07:00
AndroidUIPerformance.md Merge Android UI Performance into Performance doc, reorder sidebar 2017-03-06 10:10:04 -08:00
Animations.md Adding documentation to useNativeDriver portion of the Animated API 2017-08-04 15:41:01 -07:00
BuildingForAppleTV.md Apple TV: RCTTabBar selection controlled by native after render (fix #15081) 2017-08-10 05:36:06 -07:00
Colors.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
CommunicationAndroid.md Document Android native communication 2017-09-19 14:16:41 -07:00
CommunicationIOS.md Fix typo 2017-06-14 02:03:27 -07:00
ComponentsAndAPIs.md Breaking - Remove AdSupportIOS 2017-07-26 13:32:41 -07:00
Contributing.md Document Android native communication 2017-09-19 14:16:41 -07:00
CustomWebViewAndroid.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
CustomWebViewIOS.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
Debugging.md Add proper Stetho debugging setup documentation 2017-08-04 16:47:00 -07:00
DirectManipulation.md Replace React.createClass with create-react-class 2017-07-07 14:36:01 -07:00
GestureResponderSystem.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
GettingStarted.md Move tabbing logic back to individual docs 2017-09-01 18:06:24 -07:00
HandlingTextInput.md Retry for #14830 - Fix "Handling Text Input" tutorial's "next" within text 2017-07-24 10:15:47 -07:00
HandlingTouches.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
HeadlessJSAndroid.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
HeightAndWidth.md Update examples in docs and address version lag of CRNA 2017-05-16 23:51:28 -07:00
Images.md Update Images.md with ImageBackground 2017-07-23 08:45:44 -07:00
ImprovingUserExperience.md New guide: Improving User Experience 2017-08-24 14:01:53 -07:00
IntegrationWithExistingApps.md Update Android Guide for Integration With Existing Apps 2017-09-05 11:01:40 -07:00
JavaScriptEnvironment.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
LayoutWithFlexbox.md Update examples in docs and address version lag of CRNA 2017-05-16 23:51:28 -07:00
LinkingLibraries.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
Maintainers.md Flag large pull requests, add large-pr command 2017-08-16 21:00:44 -07:00
MoreResources.md Move CONTRIBUTING guidelines to a standalone doc in the website. 2017-07-19 10:23:06 -07:00
MoviesExample.json Fix typo and add comments to the example app 2015-03-27 20:44:20 +00:00
NativeComponentsAndroid.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
NativeComponentsIOS.md Add props for overriding native component 2017-09-19 16:01:02 -07:00
NativeMethodsMixin.md Flat ReactNative renderer bundle [WIP] 2017-05-26 11:03:06 -07:00
NativeModulesAndroid.md Added documentation for iOS app extensions 2017-07-28 12:36:14 -07:00
NativeModulesIOS.md document how to export consts from a swift native module 2017-08-03 13:13:25 -07:00
Navigation.md Update Navigation.md 2017-07-24 11:31:01 -07:00
Networking.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
Performance.md Add pertinent info for fresh flatlisters 2017-08-07 17:58:42 -07:00
PlatformSpecificInformation.md Add a section about Platform.Version on iOS 2017-06-15 09:21:18 -07:00
Props.md Standardize on using AwesomeProject for AppRegistry calls. 2017-05-30 14:20:42 -07:00
RunningOnDevice.md Move tabbing logic back to individual docs 2017-09-01 18:06:24 -07:00
RunningOnDeviceAndroid.md Consolidate Running on Device (Android|iOS) Guides into one 2016-11-06 21:13:32 -08:00
RunningOnDeviceIOS.md Consolidate Running on Device (Android|iOS) Guides into one 2016-11-06 21:13:32 -08:00
RunningOnSimulatorIOS.md Merge Android UI Performance into Performance doc, reorder sidebar 2017-03-06 10:10:04 -08:00
SignedAPKAndroid.md Update SignedAPKAndroid.md 2017-09-07 10:00:22 -07:00
State.md Standardize on using AwesomeProject for AppRegistry calls. 2017-05-30 14:20:42 -07:00
Style.md Standardize on using AwesomeProject for AppRegistry calls. 2017-05-30 14:20:42 -07:00
StyleGuide.md What to Expect from Maintainers 2017-07-28 08:18:53 -07:00
Testing.md Cleanup stray markdown files, update contributing guidelines 2017-08-04 16:18:01 -07:00
Timers.md New guide: Improving User Experience 2017-08-24 14:01:53 -07:00
TodayWidget.md Added documentation for iOS app extensions 2017-07-28 12:36:14 -07:00
Troubleshooting.md Add Troubleshooting Guide to Sidebar. 2017-08-25 15:30:43 -07:00
Tutorial.md Revert faulty change to HelloWorld demo in docs 2017-09-19 15:46:14 -07:00
UnderstandingCLI.md What to Expect from Maintainers 2017-07-28 08:18:53 -07:00
Upgrading.md Add Troubleshooting Guide to Sidebar. 2017-08-25 15:30:43 -07:00
UsingAListView.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00
UsingAScrollView.md New Handling Touches Tutorial 2017-06-07 11:51:21 -07:00