@@ -57,7 +57,10 @@ import {
5757 getInspectorDataForInstance ,
5858} from './ReactNativeFiberInspector' ;
5959
60- import { passChildrenWhenCloningPersistedNodes } from 'shared/ReactFeatureFlags' ;
60+ import {
61+ passChildrenWhenCloningPersistedNodes ,
62+ enableLazyPublicInstanceInFabric ,
63+ } from 'shared/ReactFeatureFlags' ;
6164import { REACT_CONTEXT_TYPE } from 'shared/ReactSymbols' ;
6265import type { ReactContext } from 'shared/ReactTypes' ;
6366
@@ -93,8 +96,10 @@ export type Instance = {
9396 currentProps : Props ,
9497 // Reference to the React handle (the fiber)
9598 internalInstanceHandle : InternalInstanceHandle ,
96- // Exposed through refs.
97- publicInstance : PublicInstance ,
99+ // Exposed through refs. Potentially lazily created.
100+ publicInstance : ?PublicInstance ,
101+ // This is only necessary to lazily create `publicInstance`.
102+ publicRootInstance : ?PublicRootInstance ,
98103 } ,
99104} ;
100105export type TextInstance = {
@@ -186,23 +191,37 @@ export function createInstance(
186191 internalInstanceHandle , // internalInstanceHandle
187192 ) ;
188193
189- const component = createPublicInstance (
190- tag ,
191- viewConfig ,
192- internalInstanceHandle ,
193- rootContainerInstance . publicInstance ,
194- ) ;
195-
196- return {
197- node : node ,
198- canonical : {
199- nativeTag : tag ,
194+ if ( enableLazyPublicInstanceInFabric ) {
195+ return {
196+ node : node ,
197+ canonical : {
198+ nativeTag : tag ,
199+ viewConfig,
200+ currentProps : props ,
201+ internalInstanceHandle,
202+ publicInstance : null ,
203+ publicRootInstance : rootContainerInstance . publicInstance ,
204+ } ,
205+ } ;
206+ } else {
207+ const component = createPublicInstance (
208+ tag ,
200209 viewConfig ,
201- currentProps : props ,
202210 internalInstanceHandle ,
203- publicInstance : component ,
204- } ,
205- } ;
211+ rootContainerInstance . publicInstance ,
212+ ) ;
213+
214+ return {
215+ node : node ,
216+ canonical : {
217+ nativeTag : tag ,
218+ viewConfig,
219+ currentProps : props ,
220+ internalInstanceHandle,
221+ publicInstance : component ,
222+ } ,
223+ } ;
224+ }
206225}
207226
208227export function createTextInstance (
@@ -277,7 +296,18 @@ export function getChildHostContext(
277296}
278297
279298export function getPublicInstance ( instance : Instance ) : null | PublicInstance {
280- if ( instance . canonical != null && instance . canonical . publicInstance != null ) {
299+ if ( instance . canonical != null ) {
300+ if ( instance . canonical . publicInstance == null ) {
301+ instance . canonical . publicInstance = createPublicInstance (
302+ instance . canonical . nativeTag ,
303+ instance . canonical . viewConfig ,
304+ instance . canonical . internalInstanceHandle ,
305+ instance . canonical . publicRootInstance ,
306+ ) ;
307+ // This was only necessary to create the public instance.
308+ instance . canonical . publicRootInstance = null ;
309+ }
310+
281311 return instance . canonical . publicInstance ;
282312 }
283313
0 commit comments