【问题标题】:How to import highlight.js on react component?如何在反应组件上导入 highlight.js?
【发布时间】:2019-08-01 22:34:00
【问题描述】:

我想在 React.js 组件中显示和突出显示一些“JSON”数据。

它可以工作,但问题是当组件刷新时,突出显示的代码消失了。

我想澄清一下,我是这里的新手。

感谢您的支持。

import React from "react";

import hljs from "highlight.js";
import "highlight.js/styles/github.css";
import json from "highlight.js/lib/languages/json";
hljs.registerLanguage("json", json);
hljs.initHighlightingOnLoad();

const JsonViewer = props => {
  return (
    <pre>
      <code className="json">
         {JSON.stringify(props.content, null, 2)}
      </code>
    </pre>
  );
};

export default JsonViewer;

【问题讨论】:

    标签: javascript reactjs highlight.js


    【解决方案1】:

    从我看到的函数调用hljs.initHighlightingOnLoad(); 是激活突出显示的那个。如果您使用钩子在useEffect 中调用该函数,或者如果您使用类在componentDidMountcomponentDidUpdate 中调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多