你沒辦法通過 this.props.children
取得當(dāng)前組件的子元素。 因?yàn)?code>this.props.children 返回的是組件擁有者傳遞給你的 passed onto you 子節(jié)點(diǎn)。
var App = React.createClass({
componentDidMount: function() {
// This doesn't refer to the `span`s! It refers to the children between
// last line's `<App></App>`, which are undefined.
console.log(this.props.children);
},
render: function() {
return <div><span/><span/></div>;
}
});
React.render(<App></App>, mountNode);
如果想看更多地例子, 可以參考在 front page 里最后一個(gè)例子。