【问题标题】:How to add code block for code snippets for in the data displays in the react hooks page如何在反应钩子页面的数据显示中为代码片段添加代码块
【发布时间】:2020-10-06 18:24:10
【问题描述】:

我在我的 react hooks 网站中显示来自 contentful 的博客内容。现在post.description收到了博客的完整描述。我想将 post.description 中收到的code snippets, json data 显示为code block。还想将标题添加到 h1 或 h2 标签。我们如何从描述字段中识别代码块?现在它只是在页面中显示文本(请参考添加的屏幕截图)

import React from "react";
import { Link, useParams } from "react-router-dom";
import MD from "react-markdown";
import { useSinglePost } from "../custom-hooks";
import Moment from 'moment';



export default function SinglePost() {
  const { id } = useParams();
  const [post, isLoading] = useSinglePost(id);

  const renderPost = () => {
    if (isLoading) return ( <div> <p className="noSearchData">Loading...</p> </div>);

    return (
      <React.Fragment>
        <div className="main_intro">
          <h3 className="blog_title">{post.title}</h3>
          <small className="blog_date">{Moment(post.createdAt).format('MMM DD YYYY')}</small>
          <pre className="blog_main_desc"><code>{post.description}</code></pre>
          <img
            className="blog_img"
            src={post.image.fields.file.url}
            alt={post.title}
          />
        </div>

        <div className="post__body">
          <MD source={post.body} />
        </div>
      </React.Fragment>
    );
  };

  return (
    <div className="post">
      <Link className="post__back" to="/">
        {"< Back"}
      </Link>

      {renderPost()}
    </div>
  );
}

【问题讨论】:

    标签: javascript react-hooks contentful


    【解决方案1】:

    要将代码 sn-p 作为代码块包含在 ReactJs 中,您可以使用语法高亮器,如 react-syntax-highlighter

    安装 npm react-syntax-highlighter --save

    然后在您的代码中导入荧光笔:

    import SyntaxHighLighter from 'react-syntax-highlighter';
    import { dracula } from 'react-syntax-highlighter/dist/cjs/styles/hljs';
    
    <SyntaxHighLighter language="javascript" style={dracula}>
    {
        "class HelloMessage extends React.Component {\n render() {\n return ( \n <div>\nHello {this.props.name}\n</div>\n);\n }\n}\n\nReactDOM.render(\n<HelloMessage name='Taylor' />,\ndocument.getElementById('hello-example')\n);"
      }
    </SyntaxHighLighter>
    

    【讨论】:

    • 我有post.description包含描述和sn-ps的代码,会自动识别代码。将尽快尝试并回复
    • 我已经尝试过,但我收到以下错误:预期 string 的价值,得到 [object Object] 我已添加 SyntaxHighLighter 如下...&lt;SyntaxHighLighter language="javascript" style={dracula}&gt; &lt;pre className="post__intro__desc"&gt;&lt;code&gt;{post.description}&lt;/code&gt;&lt;/pre&gt; &lt;/SyntaxHighLighter&gt;
    • 我在我的示例中使用了 React 钩子并在 post 中获取博客数据,所以您能否按照我上面的问题解释如何使用 SyntaxHighLighter
    • 添加
       标签会导致错误。你能试试 <syntaxhighlighter language="javascript" style="{dracula}"> {post.description} </syntaxhighlighter>
    • 非常感谢,现在越来越好了。我已按照建议删除了 pre 和 code 标记。收到的结果 - 现在它正在添加突出显示的所有内容..请参阅添加到上述问题的新屏幕截图。任何建议
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多