diff --git a/docs/source/about/changelog.rst b/docs/source/about/changelog.rst index b79529acc..04d426a6b 100644 --- a/docs/source/about/changelog.rst +++ b/docs/source/about/changelog.rst @@ -23,6 +23,7 @@ Unreleased - :pull:`1118` - `module_from_template` is broken with a recent release of `requests` - :pull:`1131` - `module_from_template` did not work when using Flask backend - :pull:`1200` - Fixed `UnicodeDecodeError` when using `reactpy.web.export` +- :pull:`1224` - Fixes needless unmounting of JavaScript components during each ReactPy render. **Added** diff --git a/src/js/packages/@reactpy/client/src/components.tsx b/src/js/packages/@reactpy/client/src/components.tsx index 728c4cec7..2319f81c7 100644 --- a/src/js/packages/@reactpy/client/src/components.tsx +++ b/src/js/packages/@reactpy/client/src/components.tsx @@ -177,7 +177,7 @@ function useForceUpdate() { function useImportSource(model: ReactPyVdom): MutableRefObject { const vdomImportSource = model.importSource; - + const vdomImportSourceJsonString = JSON.stringify(vdomImportSource); const mountPoint = useRef(null); const client = React.useContext(ClientContext); const [binding, setBinding] = useState(null); @@ -203,7 +203,7 @@ function useImportSource(model: ReactPyVdom): MutableRefObject { binding.unmount(); } }; - }, [client, vdomImportSource, setBinding, mountPoint.current]); + }, [client, vdomImportSourceJsonString, setBinding, mountPoint.current]); // this effect must run every time in case the model has changed useEffect(() => { diff --git a/src/py/reactpy/reactpy/web/templates/react.js b/src/py/reactpy/reactpy/web/templates/react.js index 5c6a45743..366be4fd0 100644 --- a/src/py/reactpy/reactpy/web/templates/react.js +++ b/src/py/reactpy/reactpy/web/templates/react.js @@ -17,11 +17,12 @@ export default ({ children, ...props }) => { }; export function bind(node, config) { + const root = ReactDOM.createRoot(node); return { create: (component, props, children) => React.createElement(component, wrapEventHandlers(props), ...children), - render: (element) => ReactDOM.render(element, node), - unmount: () => ReactDOM.unmountComponentAtNode(node), + render: (element) => root.render(element), + unmount: () => root.unmount() }; }