I'm using a library with a component that returns a bunch of HTML. Some of the HTML is ok, and some of it needs changed after rendering.
I'm aware this is unadvised however I am in a situation where this kind of hacky method seems necessary.
My problem is that following the dom mutations, React seems to break. Re-renders no longer occur, and there are no errors in the console to indicate why. This is obviously due to the mutations, but I think there must be some way to do this without breaking React. I've tried various things including using a ref and eg useLayoutEffect.
I have create a minimal-ish repro here:
couple of things here from your sandbox:
App
and MyComponent
when you press the buttons, you can prove it by adding a console.log("XXX has re-rendered")
in each componentMyComponent
mounts) because the dependencies never changedata
variableGoing from
<div id="data">
<p>Data: {data}</p>
</div>
to
<div id="data">
<p>Data: mutation one</p>
</div>
So every button press, your <MyComponent />
is updating and rendering the same static JSX.
Here's a modified sandbox that passes the data
state variable to the effect to cause it to trigger on button change + appendChild via ref instead replaceChild so we can see the re-renders
Some things that might be helpful:
dangerouslySetInnerHTML
(assuming it's safe to do so and is from a trusted source otherwise it opens potential exploits)<div dangerouslySetInnerHTML={{__html: data}} />
function MyComponent() {
const [data, setData] = useState("");
const handleButtonPressed = (html) => {
const fixedHtml = // ... make your changes to the HTML
setData(fixedHtml);
};
return (
<div>
<button onClick={handleButtonPressed}>some button</button>
<p>{data}</p>
</div>
);
}
}
Hope that helps, the new version of the React docs is a fantastic resource.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.