Repository navigation
Using react components in miso #1682
|
I want to use h5web and also miso, but this thing is react (with no native JS bindings). Can I still use that with Miso somehow? |
Replies: 1 comment
|
Yes. The usual pattern is to give React its own DOM node that Miso never diffs into, and mount the React tree there yourself. Miso has the hooks for exactly this (“third-party widget initialisation” in the onCreated docs).
import { createRoot } from "react-dom/client";
import { App, H5GroveProvider } from "@h5web/app";
const roots = new WeakMap();
window.mountH5Web = (el, props) => {
const root = createRoot(el);
roots.set(el, root);
root.render(
<H5GroveProvider url={props.url} filepath={props.filepath}>
<App />
</H5GroveProvider>
);
};
window.unmountH5Web = (el) => roots.get(el)?.unmount();
data Action = MountH5 DOMRef | UnmountH5 DOMRef
view _ =
div_ [ onCreatedWith MountH5
, onBeforeDestroyedWith UnmountH5
, style_ [("height", "600px")] ]
[] -- no Miso children: React owns this subtree
update (MountH5 el) = io_ $ do
win <- jsg "window"
props <- toJSVal (object ["url" .= h5Url, "filepath" .= h5File])
void $ callFunction win "mountH5Web" (el, props)
update (UnmountH5 el) = io_ $ do
win <- jsg "window"
void $ callFunction win "unmountH5Web" [el]Keep the host div_ childless. Miso won’t touch its contents, so React and Miso don’t fight over the DOM. Alternative: wrap the React component as a custom element with @r2wc/react-to-web-component, then render it like any tag with If this helps, please mark it as the answer. |
Yes. The usual pattern is to give React its own DOM node that Miso never diffs into, and mount the React tree there yourself. Miso has the hooks for exactly this (“third-party widget initialisation” in the onCreated docs).