I am using the modal library winbox. It works well if use simple html and javascript. But I can't append a React node to it.
The modal has a html
parameter, that accept an html string such as div.innerHTML = <div>hello</div>
. The code source is: this.body.innerHTML = html;
.
So adding a classic React element makes the modal crash. The only solution I found is to use react-dom's renderToString
method: html: renderToString(children)
. But the component is not dynamic anymore (no state update, etc.).
I also tried to surround React.renderDOM
by a div inside the modal and attach the component to it, as the app is attached to index.js's #root
div.
html: <div id="modal">
{render(
<div children={content} />,
document.querySelector("#modal")
)},
</div>
My question is thus: how to pass a dynamic React component to the body of this modal?
Here is my code:
import React from "react";
import useModal from "./useModal";
const Counter = () => {
const [count, setCount] = useState(1);
return (
<div>
The count is {count}
<button onClick={() => setCount(count + 1)}>+1</button>
</div>
);
};
export default function App() {
const [open, setOpen] = useState(false);
useModal(open, setOpen, <Counter />);
return (
<div id="#hi">
<button onClick={() => setOpen(!open)}>toggle</button>
</div>
);
}
// useModal
import WinBox from "winbox/src/js/winbox.js";
import "winbox/dist/css/winbox.min.css";
import React from "react";
import { render } from "react-dom";
export default function useModal(open, onToggle, content) {
const modal = open
? new WinBox({
title: "Hello",
max: true,
html: content, // INSERT DYNAMIC COMPONENT HERE
onclose: () => onToggle(false)
})
: null;
return modal;
}
Thank you!