Skip to content

Commit 55468d6

Browse files
committed
fix: unit test for pluggable component
1 parent 7fee38c commit 55468d6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/components/PluggableComponent/index.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
2-
import React, { useState, useEffect } from 'react';
2+
import React, { useState, useEffect, useRef } from 'react';
33
import loadable from '@loadable/component';
44
import PropTypes from 'prop-types';
55
import 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

Comments
 (0)