【发布时间】:2020-10-28 20:38:05
【问题描述】:
我正在尝试在 React 组件中使用 ReactMarkdown 呈现一些链接,这需要传递一些道具以使用特殊样式。
Links 是我的样式化组件,我将其应用于ReactMarkdown 中renderers 下的paragraph 属性。但是Links 工作我需要通过linkColor={linkcColor} props。
代码:我的部分代码是Container 是另一个styled.div
<Container>
{websites.map((website, index) => (
<div key={'website' + index}>
<ReactMarkdown
source={`[${website.websiteName}](${website.externalUrl})`}
unwrapDisallowed={true}
renderers={{ paragraph: Links, link: Linkrender }}
/>
</div>
))}
</Container>
const Links = styled.div`
color: ${(p) => p.linkColor};
::before {
content: ' ';
white-space: pre;
}
::after {
content: ' /';
white-space: pre;
}
`;
尝试过:以下。但它没有奏效。它完全失去了ReactMarkdown 的source,而只是将style 应用于空的div。
<ReactMarkdown
source={`[${website.websiteName}](${website.externalUrl})`}
unwrapDisallowed={true}
renderers={{ paragraph: ({linkColor}) => (
<Links {...linkColor={linkColor}} />), <<<<<<<<<<<<< tried this
link: Linkrender }}
/>
语法正确吗?这不起作用的原因可能是什么?是因为我的样式组件是div,它被应用在<p> 上吗?这里Linkrender 是另一个定制的样式组件<a>,我无法更改。
【问题讨论】: