MountingManager can create views with props in one step instead of two

Summary: Create views with props in one call instead of two. Backwards-compatible.

Reviewed By: shergin

Differential Revision: D14846424

fbshipit-source-id: cb53225579089f7e51d4e9d1fc9fc2e331a994c1
This commit is contained in:
Joshua Gross
2019-04-15 01:41:47 -07:00
committed by Facebook Github Bot
parent b60651dd09
commit bbd925cdd1
8 changed files with 59 additions and 23 deletions

View File

@@ -42,10 +42,20 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
/**
* Creates a view and installs event emitters on it.
*/
public final @Nonnull T createView(
private final @Nonnull T createView(
@Nonnull ThemedReactContext reactContext,
JSResponderHandler jsResponderHandler) {
T view = createViewInstance(reactContext);
return this.createViewWithProps(reactContext, null, jsResponderHandler);
}
/**
* Creates a view with knowledge of props.
*/
public @Nonnull T createViewWithProps(
@Nonnull ThemedReactContext reactContext,
ReactStylesDiffMap props,
JSResponderHandler jsResponderHandler) {
T view = createViewInstanceWithProps(reactContext, props);
addEventEmitters(reactContext, view);
if (view instanceof ReactInterceptingViewGroup) {
((ReactInterceptingViewGroup) view).setOnInterceptTouchEventListener(jsResponderHandler);
@@ -53,6 +63,7 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
return view;
}
/**
* @return the name of this view manager. This will be the name used to reference this view
* manager from JavaScript in createReactNativeComponentClass.
@@ -90,6 +101,20 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>
*/
protected abstract @Nonnull T createViewInstance(@Nonnull ThemedReactContext reactContext);
/**
* Subclasses should return a new View instance of the proper type.
* This is an optional method that will call createViewInstance for you.
* Override it if you need props upon creation of the view.
* @param reactContext
*/
protected @Nonnull T createViewInstanceWithProps(@Nonnull ThemedReactContext reactContext, ReactStylesDiffMap initialProps) {
T view = createViewInstance(reactContext);
if (initialProps != null) {
updateProperties(view, initialProps);
}
return view;
}
/**
* Called when view is detached from view hierarchy and allows for some additional cleanup by
* the {@link ViewManager} subclass.