簡體   English   中英

如何在 React 中獲取子源代碼?

[英]How can I get children source code in React?

react 有沒有可能得到 children 的 prop 源碼?

例如,我有帶有某種代碼的組件...

<SomeComp>
    <Heading>I need this code with Heading tag</Heading>
</SomeComp>

我怎樣才能讓孩子們成為一個字符串?

我需要這樣的東西...

const SomeComp = ({ children }) => {
    const highlightedCode = highlight(CHILDREN_TO_SOURCE_STRING_FUNCTION(children))
    return (
        <>
            <div>{highlightedCode}</div>
            <div>{children}</div>
        </>
    )
}

謝謝幫助

不確定您真正想要在這里實現什么,但這是您可以做的。

<SomeComp>
  {`<Heading>I need this code with Heading tag</Heading>`}
</SomeComp>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM