React 不支持此功能。 React 在真正的 HTML 中不起作用;它在函数调用中起作用。尽管 JSX 看起来像 HTML,但它在由 React 运行之前会被编译为函数调用。这意味着 React 不知道如何处理真正的 HTML,就像您在示例中传递的那样。
JSX:
<div className="foo">
Here's some content
</div>
尽管这看起来与您尝试注入的内容相似,但客户端最终会看到以下内容:
React:
React.DOM.div({className: "foo"},
"Here's some content"
)
Your react_component
助手应该接受 JSX(而不是 HTML)并通过 JSX 转换器运行它,然后再将其渲染到页面。然后您可以将结果传递给组件并将其引用为this.props.children
.
这就是你的react_component
应该为您的示例输出:
React.renderComponent(
TestComponent(null,
React.DOM.div({className: "foo"},
"Here's some content"
)
),
referenceToDomNode // Not obvious where you pass this with your helper
);
和你的TestComponent
可能看起来像这样:
var TestComponent = React.createClass({
render: function() {
return (
<div className="my-component">
{this.props.children}
</div>
);
}
});