React v0.14支持纯函数组件(即相同的输入等于相同的输出)。属性作为函数参数传递。
// 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}
吗? - Ruan Mendes