rt
var per = React.createClass({
getInitialState:function(){
return (styles = {
width:'100%',
height:'100%',
background:'red',
position:'fixed'
})
},
componentWillMount:function(){
alert('ok')
},
render:function(){
return (
<p className = 'container' style={this.state}>{this.props}</p>
)
},
componentDidMount:function(){
alert('OK')
}
});
React.render(
<per name = 'myname'/>,
document.querySelector('.wrap')
componentWillMount与componentDidMount这两个函数的作用是什么?还望大家赐教
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
will是在render之前调用,did是在render之后调用,具体有什么用要看你在function里面怎么写。比如,will里面请求数据,did在告诉服务器render已经完成
正确的姿势是去翻翻react的官方文档:https://facebook.github.io/react/docs/component-specs.html 里面详细讲了react component的lifecycle
官方文档有详细说明,两者主要区别是调用顺序,一个在

render
之前,一个在render
之后。另外,官方建议是,用constructor()
替换componentWillMount