ReactDom.render 返回的类型与我需要的不同,如何解决?

huangapple go评论66阅读模式
英文:

ReactDom.render is returning different type than what I need, how to address it?

问题

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 - error TS2322: Type '({ children, isParseRequired }: { children: string; isParseRequired?: boolean | undefined; }) => void | Element' is not assignable to type 'FC<{ children: string; isParseRequired?: boolean | undefined; }>'.

注意:我正在使用 *.tsx 文件。

英文:

Markdown rendering is the primary focus. In addition I need to parse the html which is passed as string as well.

You can assume, children is a html passed as string
and isParseRequired is passed as true

import cx from &#39;classnames&#39;
import &#39;github-markdown-css&#39;
import &#39;katex/dist/katex.min.css&#39;
import { FC, ReactNode, useEffect, useMemo, useState } from &#39;react&#39;
import { CopyToClipboard } from &#39;react-copy-to-clipboard&#39;
import { BsClipboard } from &#39;react-icons/bs&#39;
import ReactDom from &#39;react-dom&#39;
import ReactMarkdown from &#39;react-markdown&#39;
import reactNodeToString from &#39;react-node-to-string&#39;
import rehypeHighlight from &#39;rehype-highlight&#39;
import remarkBreaks from &#39;remark-breaks&#39;
import remarkGfm from &#39;remark-gfm&#39;
import remarkMath from &#39;remark-math&#39;
import supersub from &#39;remark-supersub&#39;
import Tooltip from &#39;../Tooltip&#39;
import &#39;./markdown.css&#39;
const Markdown: FC&lt;{ children: string, isParseRequired?: boolean}&gt; = ({ children, isParseRequired = false }) =&gt; {
return ReactDom.render(
&lt;ReactMarkdown
remarkPlugins={[remarkMath, supersub, remarkBreaks, remarkGfm]}
rehypePlugins={[[rehypeHighlight, { detect: true, ignoreMissing: true }]]}
className={`markdown-body markdown-custom-styles !text-base font-normal`}
linkTarget=&quot;_blank&quot;
components={{
a: ({ node, ...props }) =&gt; {
if (!props.title) {
return &lt;a {...props} /&gt;
}
return (
&lt;Tooltip content={props.title}&gt;
&lt;a {...props} title={undefined} /&gt;
&lt;/Tooltip&gt;
)
},
code: ({ node, inline, className, children, ...props }) =&gt; {
if (inline) {
return (
&lt;code className={className} {...props}&gt;
{children}
&lt;/code&gt;
)
}
return &lt;CustomCode className={className}&gt;{children}&lt;/CustomCode&gt;
},
}}
children={children}/&gt;,
document.body)
}
export default Markdown

Error I am getting is :
src/app/components/Markdown/index.tsx:48:7 - error TS2322: Type &#39;({ children, isParseRequired }: { children: string; isParseRequired?: boolean | undefined; }) =&gt; void | Element&#39; is not assignable to type &#39;FC&lt;{ children: string; isParseRequired?: boolean | undefined; }&gt;&#39;.

Note: I am using *.tsx

PS: Originally posted at https://github.com/orgs/remarkjs/discussions/1188

答案1

得分: 1

移除 ReactDom.render 并用 fragment 标签包裹 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 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;
英文:

Remove ReactDom.render and wrap ReactMarkdown with fragment tag to return FC and use rehype-raw plugin

import cx from &quot;classnames&quot;;
import &quot;github-markdown-css&quot;;
import &quot;katex/dist/katex.min.css&quot;;
import { FC, ReactNode, useEffect, useMemo, useState } from &quot;react&quot;;
import { CopyToClipboard } from &quot;react-copy-to-clipboard&quot;;
import { BsClipboard } from &quot;react-icons/bs&quot;;
import ReactDom from &quot;react-dom&quot;;
import ReactMarkdown from &quot;react-markdown&quot;;
import reactNodeToString from &quot;react-node-to-string&quot;;
import rehypeHighlight from &quot;rehype-highlight&quot;;
import remarkBreaks from &quot;remark-breaks&quot;;
import remarkGfm from &quot;remark-gfm&quot;;
import remarkMath from &quot;remark-math&quot;;
import supersub from &quot;remark-supersub&quot;;
import Tooltip from &quot;../Tooltip&quot;;
import &quot;./markdown.css&quot;;
import rehypeRaw from &quot;rehype-raw&quot;; 
const Markdown: FC&lt;{ children: string, isParseRequired?: boolean }&gt; = ({
children,
isParseRequired = false,
}) =&gt; {
return (
&lt;&gt;
&lt;ReactMarkdown
remarkPlugins={[remarkMath, supersub, remarkBreaks, remarkGfm]}
rehypePlugins={[
[rehypeHighlight, { detect: true, ignoreMissing: true }], rehypeRaw
]}
className={`markdown-body markdown-custom-styles !text-base font-normal`}
linkTarget=&quot;_blank&quot;
components={{
a: ({ node, ...props }) =&gt; {
if (!props.title) {
return &lt;a {...props} /&gt;;
}
return (
&lt;Tooltip content={props.title}&gt;
&lt;a {...props} title={undefined} /&gt;
&lt;/Tooltip&gt;
);
},
code: ({ node, inline, className, children, ...props }) =&gt; {
if (inline) {
return (
&lt;code className={className} {...props}&gt;
{children}
&lt;/code&gt;
);
}
return &lt;CustomCode className={className}&gt;{children}&lt;/CustomCode&gt;;
},
}}
children={children}
/&gt;
&lt;/&gt;
);
};
export default Markdown;

huangapple
  • 本文由 发表于 2023年7月17日 11:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定