成都网站建设设计

将想法与焦点和您一起共享

React中props与state的区别-创新互联

首先,props与state是React组件的两种方法。

成都创新互联主要从事网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务姑苏,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
  1. props,可以在组件中来获取this.props的属性。

var Helloreact=React.createClass({
    render:function(){
        return 

Hello {this.props.name}

        } }); ReactDOM.render(     ,         document.getElementById('example2') );      //Hello BOOM

2.state,获取的是更新后的数据,是通过用户的状态来更改state。

var Helloreact=React.createClass({
    getInitialState : function(){
        return {name:'BOOM'};
        },
    render:function(){
        return 

Hello {this.state.name}

        } }); ReactDOM.render(     ,         document.getElementById('example2') );    //Hello BOOM

3.在这里,可以通过props获取组件的属性,然后用state动态的更新。

 var HelloMe = React.createClass({
                getDefaultProps:function(){
                    return{
                        value:'props'
                    };
                },
                getInitialState : function(){
                    return {value:'state'};
                },
                handleChange:function(event){
                    this.setState({value:event.target.value});
                },
                clickhandle:function(event){
                    this.setState({value:" "});
                },
                render:function(){
                    var value= this.state.value;
                    return  
                                                         

Hi {this.props.value} {value}

                            清除{value}                             
;                 }             });             ReactDOM.render(                 
,                 document.getElementById('example1')             );

所以言之,相对于静态的状态下使用props会更好一些,动态的数据就需要使用state,

而React中,是虚拟的DOM树,是遍历全局后对数据进行对比,然后运算使用最快的方法进行的渲染。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:React中props与state的区别-创新互联
网页URL:http://chengdu.cdxwcx.cn/article/pohph.html

其他资讯