this.props.children undefined

2019-08-14 14:31 更新

你沒辦法通過 this.props.children 取得當前組件的子元素。 因為this.props.children 返回的是組件擁有者傳遞給你的 passed onto you 子節(jié)點。

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 里最后一個例子。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號