在尝试这种情况后,我发现您实际上可以传递使用功能组件并返回字符串:https://github.com/missive/emoji-mart#using-with-dangerouslysetinnerhtml https://github.com/missive/emoji-mart#using-with-dangerouslysetinnerhtml(具体针对我的问题emoji-mart
)
所以我对其他组件所做的事情是相同的,我没有调用 React 组件,而是创建了一个函数:
function testComponent(props) {
const { style, className, children, html } = props;
if (html) {
return `<span style='${style}' class='${className}'>${children || ''}</span>`;
}
return (
<span style="${style}" class="${className}">
${children || ''}
</span>
);
}
并将其称为:
function testComponent(props) {
const { content } = props; // content is a markdown and can be a stringified image tag
return testComponent({ children: content, html: true });
}
而对于dangerouslySetInnerHTML
:
(反应组件内的渲染函数)
render() {
const props = {
dangerouslySetInnerHTML: {
__html: testComponent(this.props.content),
},
};
return React.createElement('div', props);
}
这比使用更黑客,但更便宜:
renderToString()
renderToStaticMarkup()