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.
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.
2 Answers
Reset to default 5katex.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} />
}
This is a modified version of @Quentin code for React with TypeScript:
import katex from "katex";
import { useEffect, useRef } from "react";
import "katex/dist/katex.min.css";
function KaTeX({ texExpression, className }: { texExpression: string, className: string }) {
const containerRef = useRef<HTMLInputElement>(null);
useEffect(() => {
katex.render(texExpression, containerRef.current as HTMLInputElement);
}, [texExpression]);
return <div ref={containerRef} className={className} />;
}
export default KaTeX;
And this is what you need for installation npm i katex @types/katex
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745332525a4622947.html
评论列表(0条)