我有一个问题,关于componentDidMount 何时触发,我刚开始接触React,并且文档中的例子很模糊。为什么我的组件没有被触发?
var App = React.createClass({
getInitialState: function() {
var events = ['initial state has been called'];
return {
events: events
}
},
componentWillMount: function() {
return {
mount: this.state.events.push('componentWillMount has been called')
}
},
componentDidMount: function() {
return
console.log('mounted!');
},
render: function() {
var eventMap = this.state.events.map(function(event, i) {
return <li key={i}>{event}</li>;
})
return (
<div>
<ul>
{eventMap}
</ul>
</div>
)
}
})
ReactDOM.render(
<App />,
document.getElementById('container')
)
console.log
语句在return
之后。所以componentDidMount
函数在console.log
之前返回。 - zerkmscomponentWillMount
不应该返回任何东西(并且在文档中明确说明了)。 - zerkms