【发布时间】:2016-05-06 13:37:54
【问题描述】:
React v0.14 支持pure functional components(即相同的输入等于相同的输出)。道具作为函数参数传入。
// Using ES6 arrow functions and an implicit return:
const PureComponent = ({url}) => (
<div>
<a href={url}>{url}</a>
</div>
);
// Used like this:
<PureComponent url="http://www.google.ca" />
// Renders this:
<a href="http://www.google.ca">http://www.google.ca</a>
但是如何渲染 PureComponent 的子组件?在常规的有状态组件中,您使用 this.props.children 访问子组件,但这显然在这里不起作用。
const PureComponent = ({url}) => (
<div>
<a href={url}>{children}</a> // <--- This doesn't work
</div>
);
<PureComponent url="http://www/google.ca">Google Canada</PureComponent>
// I want to render this:
<a href="http://www.google.ca">Google Canada</a>
我该怎么办?
【问题讨论】:
-
应该是
{props.children}?
标签: javascript reactjs