If you using react hooks please follow this;
I get the solution by integrate react-highlight.
You can use innerHtml
to true on the property.
import Highlight from "react-highlight"
import "highlight.js/styles/gradient-dark.css"; // import style on the root file
import JSONViewer from "./JSONViewer"
<Highlight innerHTML className="w-full">
<JSONViewer content={yourContentHere} />
</Highlight>
Set innerHTML=true
to highlight multiple code snippets at a time. This is especially usefull if html with multiple code snippets is generated from preprocesser tools like markdown.
Warning: If innerHTML is set to true, make sure the html generated
with code snippets is from trusted source.
Trying to forcing it on useEffect?
Using hljs.registerLanguage("json", json);
or hljs.initHighlightingOnLoad();
will getting a problem by rerendering react. It's not recall when using dependency but useEffect
without dependency like this it works.
useEffect(() => {
hljs.initHighlightingOnLoad(); // it works, but bad for performance because will call every single time re-rendering.
})
useEffect(() => {
hljs.initHighlightingOnLoad(); // it doesn't works
}, [])