11/* eslint-disable react-hooks/exhaustive-deps */
2- import React , { useState , useEffect } from 'react' ;
2+ import React , { useState , useEffect , useRef } from 'react' ;
33import loadable from '@loadable/component' ;
44import PropTypes from 'prop-types' ;
55import useDeepCompareEffect from 'use-deep-compare-effect' ;
@@ -23,6 +23,7 @@ const PluggableComponent = ({
2323 ...pluggableComponentProps
2424} ) => {
2525 const [ newComponent , setNewComponent ] = useState ( children || null ) ;
26+ const loadedComponentRef = useRef ( null ) ;
2627
2728 useEffect ( ( ) => {
2829 const loadPluginComponent = async ( ) => {
@@ -41,6 +42,7 @@ const PluggableComponent = ({
4142 ) ;
4243
4344 setNewComponent ( component ) ;
45+ loadedComponentRef . current = true ;
4446 }
4547 } catch ( error ) {
4648 console . error ( `Failed to load plugin ${ as } :` , error ) ;
@@ -51,14 +53,14 @@ const PluggableComponent = ({
5153 } , [ id , as ] ) ;
5254
5355 useEffect ( ( ) => {
54- if ( newComponent && children ) {
56+ if ( newComponent && children && loadedComponentRef . current ) {
5557 const updatedComponent = React . cloneElement ( newComponent , pluggableComponentProps , children ) ;
5658 setNewComponent ( updatedComponent ) ;
5759 }
5860 } , [ children ] ) ;
5961
6062 useDeepCompareEffect ( ( ) => {
61- if ( newComponent ) {
63+ if ( newComponent && loadedComponentRef . current ) {
6264 const updatedComponent = React . cloneElement ( newComponent , pluggableComponentProps , children ) ;
6365 setNewComponent ( updatedComponent ) ;
6466 }
0 commit comments