2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-17 18:26:32 +00:00

Fix for <RemoteComponent> (#10007)

- Prevent multiple creation of root node
- Fixes error message in browser console
This commit is contained in:
Oliver
2025-07-11 21:33:36 +10:00
committed by GitHub
parent d3f24bc529
commit fa62c9500b

View File

@@ -32,13 +32,13 @@ export default function RemoteComponent({
context: InvenTreePluginContext; context: InvenTreePluginContext;
}>) { }>) {
const componentRef = useRef<HTMLDivElement>(); const componentRef = useRef<HTMLDivElement>();
const [rootElement, setRootElement] = useState<Root | null>(null); const rootElement = useRef<Root | null>(null);
useEffect(() => { useEffect(() => {
if (componentRef.current && !rootElement) { if (componentRef.current && rootElement.current === null) {
setRootElement(createRoot(componentRef.current)); rootElement.current = createRoot(componentRef.current);
} }
}, [componentRef.current]); }, [rootElement]);
const [renderingError, setRenderingError] = useState<string | undefined>( const [renderingError, setRenderingError] = useState<string | undefined>(
undefined undefined
@@ -58,7 +58,7 @@ export default function RemoteComponent({
}, [source, defaultFunctionName]); }, [source, defaultFunctionName]);
const reloadPluginContent = useCallback(() => { const reloadPluginContent = useCallback(() => {
if (!rootElement) { if (!rootElement.current) {
return; return;
} }
@@ -80,7 +80,7 @@ export default function RemoteComponent({
// Render the plugin component into the target element // Render the plugin component into the target element
// Note that we have to provide the right context(s) to the component // Note that we have to provide the right context(s) to the component
// This approach ensures that the component is rendered in the correct context tree // This approach ensures that the component is rendered in the correct context tree
rootElement.render( rootElement.current?.render(
<ApiProvider client={queryClient} api={api}> <ApiProvider client={queryClient} api={api}>
<MantineProvider <MantineProvider
theme={ctx.theme} theme={ctx.theme}
@@ -116,7 +116,7 @@ export default function RemoteComponent({
// Reload the plugin content dynamically // Reload the plugin content dynamically
useEffect(() => { useEffect(() => {
reloadPluginContent(); reloadPluginContent();
}, [source, func, context, rootElement]); }, [func, context, rootElement.current]);
return ( return (
<Boundary label={identifierString(`RemoteComponent-${func}`)}> <Boundary label={identifierString(`RemoteComponent-${func}`)}>