From 76866b53d838539d58d4293cb377ba8c728a5722 Mon Sep 17 00:00:00 2001 From: Tugdual de Kerviler Date: Thu, 2 Apr 2020 10:56:11 +0200 Subject: [PATCH] Register component directly in render for more flexibility --- packages/edit-post/src/index.native.js | 6 ++++-- packages/element/src/react-platform.native.js | 13 ++++++++----- packages/react-native-editor/src/index.js | 7 ++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/edit-post/src/index.native.js b/packages/edit-post/src/index.native.js index 1f72de810287e..8efafa0714d63 100644 --- a/packages/edit-post/src/index.native.js +++ b/packages/edit-post/src/index.native.js @@ -22,6 +22,7 @@ let blocksRegistered = false; * that can be registered with `AppRegistry.registerComponent` */ export function initializeEditor( { + id, initialHtml, initialTitle, initialHtmlModeEnabled, @@ -35,12 +36,13 @@ export function initializeEditor( { registerCoreBlocks(); blocksRegistered = true; - return render( + render( + />, + id ); } diff --git a/packages/element/src/react-platform.native.js b/packages/element/src/react-platform.native.js index d4f65b71857cb..b821b04f40ae4 100644 --- a/packages/element/src/react-platform.native.js +++ b/packages/element/src/react-platform.native.js @@ -1,6 +1,7 @@ /** * External dependencies */ +import { AppRegistry } from "react-native"; import { isEmpty, omit } from 'lodash'; /** @@ -13,23 +14,25 @@ import { applyFilters, doAction } from '@wordpress/hooks'; */ import { cloneElement } from './react'; -const render = ( element ) => ( propsFromNative ) => { - doAction( 'native.render', propsFromNative ); +const render = ( element, id ) => AppRegistry.registerComponent( id, () => ( propsFromNative ) => { + const nativeProps = omit( propsFromNative || {}, [ 'rootTag' ] ); + + doAction( 'native.render', nativeProps ); // if we have not received props from a parent native app // just render the element as it is - if ( isEmpty( omit( propsFromNative, [ 'rootTag' ] ) ) ) { + if ( isEmpty( nativeProps ) ) { return element; } // Otherwise overwrite the existing props using a filter hook const filteredProps = applyFilters( 'native.block_editor_props', - propsFromNative + nativeProps ); return cloneElement( element, filteredProps ); -}; +} ); /** * Render a given element on Native. diff --git a/packages/react-native-editor/src/index.js b/packages/react-native-editor/src/index.js index c5e51aa9a1577..523d2d5f93b34 100644 --- a/packages/react-native-editor/src/index.js +++ b/packages/react-native-editor/src/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { AppRegistry, I18nManager } from 'react-native'; +import { I18nManager } from 'react-native'; /** * Internal dependencies @@ -36,7 +36,8 @@ const gutenbergSetup = () => { setupInitHooks(); const initializeEditor = require( '@wordpress/edit-post' ).initializeEditor; - return initializeEditor( { + initializeEditor( { + id: 'gutenberg', initialHtml, initialHtmlModeEnabled: false, initialTitle: 'Welcome to Gutenberg!', @@ -89,4 +90,4 @@ const setupLocale = ( locale, extraTranslations ) => { reactNativeSetup(); -AppRegistry.registerComponent( 'gutenberg', gutenbergSetup ); +gutenbergSetup();