【问题标题】:Typing Custom Renderers [ Gatsby & Contenful Richtext ]键入自定义渲染器 [ Gatsby 和内容丰富的富文本]
【发布时间】:2020-09-09 04:18:15
【问题描述】:

很长一段时间以来,我一直在尝试键入我的 gatsby 内容丰富的富文本自定义渲染器,但不幸的是没有取得多大成功。

有人能引导我走上正确的道路吗?如何在我的 const Bold 和 const Text 中键入 {children} 而无需这种阴暗的“任何”黑客攻击?

const Bold = ({ children }: any) => <p className="bold">{children}</p>
const Text = ({ children }: any) => <p className="paragraph">{children}</p>

const options = {
  renderMark: {
    [MARKS.BOLD]: (text: ReactNode) => <Bold>{text}</Bold>,
  },
  renderNode: {
    [BLOCKS.PARAGRAPH]: (node: ReactNode, children: ReactNode) => (
      <Text>{children}</Text>
    ),
  },
}

提前 THX 并从维也纳向您致以最诚挚的问候

【问题讨论】:

    标签: javascript typescript gatsby contentful


    【解决方案1】:

    正确的足迹应该是({ children }: { children: React.ReactChildren }) =&gt;...,因为孩子是 React Children,并且它不期望其他 React 提供的属性。如果您需要来自 Props 对象的其他 React 属性和自定义属性,您可以使用 React.propsWithChildren&lt;T&gt; 类型键入它。

    const Bold = ({ children }: { children: React.ReactChildren }) => <p className="bold">{children}</p>
    const Text = ({ children }: { children: React.ReactChildren }) => <p className="paragraph">{children}</p>
    

    【讨论】:

    • 感谢罗伯特您的回答,您已指导我找到最终解决方案。我已经用实际可以正常工作的代码编辑了您的答案。
    • 如果我的答案是正确的,那么请务必接受它作为正确的答案,以便人们知道
    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2019-11-01
    • 1970-01-01
    • 2021-11-26
    • 2011-06-11
    相关资源
    最近更新 更多