搜索
javascript - react中 componentWillMount,componentDidMount有什么用处?
PHPz
PHPz 2017-04-10 16:30:44
[JavaScript讨论组]

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这两个函数的作用是什么?还望大家赐教

PHPz
PHPz

学习是最好的投资!

全部回复(3)
怪我咯

will是在render之前调用,did是在render之后调用,具体有什么用要看你在function里面怎么写。比如,will里面请求数据,did在告诉服务器render已经完成

PHP中文网

正确的姿势是去翻翻react的官方文档:https://facebook.github.io/react/docs/component-specs.html 里面详细讲了react component的lifecycle

伊谢尔伦

官方文档有详细说明,两者主要区别是调用顺序,一个在render之前,一个在render之后。另外,官方建议是,用constructor()替换componentWillMount

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号