1 year ago

#70301

test-img

Gouri Nakate

How to render MAthml tags in reactjs from string?

My code is not working. It's also showing a warning for "react-mathjax-preview":

Could not find a declaration file for module 'react-mathjax-preview'.

My code is:

import React from "react";
import MathJax from "react-mathjax-preview";
import ReactHtmlParser from "react-html-parser";

export const renderText = (text) => {
  const mathtype = <MathJax math={String.raw`${text}`} />;
  const texttype = ReactHtmlParser(text);

  let textType = "";

  const loop = (string) =>
    string.forEach((item) => {
      if (item?.type !== "math") {
        if (item?.props?.children?.length > 0) {
          return loop(item?.props?.children);
        }
      } else {
        textType = "math";
      }
    });

  loop(texttype);

  if (textType !== "math") {
    return texttype;
  } else {
    return mathtype;
  }
};

reactjs

mathml

0 Answers

Your Answer

Accepted video resources