Skip to content

Commit 0e4432f

Browse files
authored
Fixes needless custom component recreation (#1195) (#1224)
1 parent f70bccd commit 0e4432f

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

Diff for: docs/source/about/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Unreleased
2323
- :pull:`1118` - `module_from_template` is broken with a recent release of `requests`
2424
- :pull:`1131` - `module_from_template` did not work when using Flask backend
2525
- :pull:`1200` - Fixed `UnicodeDecodeError` when using `reactpy.web.export`
26+
- :pull:`1224` - Fixes needless unmounting of JavaScript components during each ReactPy render.
2627

2728
**Added**
2829

Diff for: src/js/packages/@reactpy/client/src/components.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function useForceUpdate() {
177177

178178
function useImportSource(model: ReactPyVdom): MutableRefObject<any> {
179179
const vdomImportSource = model.importSource;
180-
180+
const vdomImportSourceJsonString = JSON.stringify(vdomImportSource);
181181
const mountPoint = useRef<HTMLElement>(null);
182182
const client = React.useContext(ClientContext);
183183
const [binding, setBinding] = useState<ImportSourceBinding | null>(null);
@@ -203,7 +203,7 @@ function useImportSource(model: ReactPyVdom): MutableRefObject<any> {
203203
binding.unmount();
204204
}
205205
};
206-
}, [client, vdomImportSource, setBinding, mountPoint.current]);
206+
}, [client, vdomImportSourceJsonString, setBinding, mountPoint.current]);
207207

208208
// this effect must run every time in case the model has changed
209209
useEffect(() => {

Diff for: src/py/reactpy/reactpy/web/templates/react.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ export default ({ children, ...props }) => {
1717
};
1818

1919
export function bind(node, config) {
20+
const root = ReactDOM.createRoot(node);
2021
return {
2122
create: (component, props, children) =>
2223
React.createElement(component, wrapEventHandlers(props), ...children),
23-
render: (element) => ReactDOM.render(element, node),
24-
unmount: () => ReactDOM.unmountComponentAtNode(node),
24+
render: (element) => root.render(element),
25+
unmount: () => root.unmount()
2526
};
2627
}
2728

0 commit comments

Comments
 (0)