Home > database >  How can I use KaTeX in React project?
How can I use KaTeX in React project?

Time:02-23

How can KaTex be used in a React project? I read through documentation on NPM and still don't get it. I don't quite see how katex.render and katex.renderToString can be applied to React. Thanks in advanced!

CodePudding user response:

katex.render needs a DOM element to render in, you can get one with a useRef hook.

const KaTeXComponent = ({texExpression}) => {
    const containerRef = useRef();

    useEffect(() => {
        katex.render(texExpression, containerRef.current);
    }, [texExpression]);

    return <div ref={containerRef} />
}
  • Related