如何解決ReactDom.render返回的類型與所需類型不符的問題?
P粉289775043
P粉289775043 2023-09-09 11:11:26
0
1
644

Markdown渲染是主要關注點。此外,我還需要解析作為字串傳遞的html。

您可以假設children是作為字串傳遞的html, isParseRequired被傳遞為true

import cx from 'classnames'
import 'github-markdown-css'
import 'katex/dist/katex.min.css'
import { FC, ReactNode, useEffect, useMemo, useState } from 'react'
import { CopyToClipboard } from 'react-copy-to-clipboard'
import { BsClipboard } from 'react-icons/bs'
import ReactDom from 'react-dom'
import ReactMarkdown from 'react-markdown'
import reactNodeToString from 'react-node-to-string'
import rehypeHighlight from 'rehype-highlight'
import remarkBreaks from 'remark-breaks'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import supersub from 'remark-supersub'
import Tooltip from '../Tooltip'
import './markdown.css'

const Markdown: FC<{ children: string, isParseRequired?: boolean}> = ({ children, isParseRequired = false }) => {
    return ReactDom.render(
      <ReactMarkdown
        remarkPlugins={[remarkMath, supersub, remarkBreaks, remarkGfm]}
        rehypePlugins={[[rehypeHighlight, { detect: true, ignoreMissing: true }]]}
        className={`markdown-body markdown-custom-styles !text-base font-normal`}
        linkTarget="_blank"
        components={{
          a: ({ node, ...props }) => {
            if (!props.title) {
              return <a {...props} />
            }
            return (
              <Tooltip content={props.title}>
                <a {...props} title={undefined} />
              </Tooltip>
            )
          },
          code: ({ node, inline, className, children, ...props }) => {
            if (inline) {
                return (
                  <code className={className} {...props}>
                    {children}
                  </code>
                )
            }
            return <CustomCode className={className}>{children}</CustomCode>
          },
        }}
        children={children}/>,
        document.body)
}

export default Markdown

我得到的錯誤是: src/app/components/Markdown/index.tsx:48:7 - 錯誤TS2322: 類型'({children, isParseRequired }: {children: string; isParseRequired?: boolean | undefined; }) => void | ' 不可指派給類型'FC<{children: string; isParseRequired?: 布尔值 |不明确的; }>'。

注意:我正在使用*.tsx

PS:最初發佈在https://github.com/orgs/remarkjs/discussions/1188

P粉289775043
P粉289775043

全部回覆(1)
P粉162773626

刪除ReactDom.render並將ReactMarkdown包裝在片段標籤中以返回FC並使用rehype-raw插件

import cx from "classnames";
import "github-markdown-css";
import "katex/dist/katex.min.css";
import { FC, ReactNode, useEffect, useMemo, useState } from "react";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { BsClipboard } from "react-icons/bs";
import ReactDom from "react-dom";
import ReactMarkdown from "react-markdown";
import reactNodeToString from "react-node-to-string";
import rehypeHighlight from "rehype-highlight";
import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import supersub from "remark-supersub";
import Tooltip from "../Tooltip";
import "./markdown.css";
import rehypeRaw from "rehype-raw"; 

const Markdown: FC<{ children: string, isParseRequired?: boolean }> = ({
  children,
  isParseRequired = false,
}) => {
  return (
    <>
      <ReactMarkdown
        remarkPlugins={[remarkMath, supersub, remarkBreaks, remarkGfm]}
        rehypePlugins={[
          [rehypeHighlight, { detect: true, ignoreMissing: true }], rehypeRaw
        ]}
        className={`markdown-body markdown-custom-styles !text-base font-normal`}
        linkTarget="_blank"
        components={{
          a: ({ node, ...props }) => {
            if (!props.title) {
              return <a {...props} />;
            }
            return (
              <Tooltip content={props.title}>
                <a {...props} title={undefined} />
              </Tooltip>
            );
          },
          code: ({ node, inline, className, children, ...props }) => {
            if (inline) {
              return (
                <code className={className} {...props}>
                  {children}
                </code>
              );
            }
            return <CustomCode className={className}>{children}</CustomCode>;
          },
        }}
        children={children}
      />
    </>
  );
};

export default Markdown;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板