美文网首页
state,props,refs属性

state,props,refs属性

作者: QinRenMin | 来源:发表于2018-07-25 21:49 被阅读0次

    react的三大属性state props refs

    • state
      状态三部曲:
      1)初始化状态值
      在constructor中初始化
    this.state = {
      name:value
    }
    

    2)读取state值

    const {name} = this.state;
    

    3)更新状态值(在定义的函数内)

    const name = this.state.name;
    this.setState({name})
    
    • props
      指定默认属性
    Mycomponent.defaultProps={
      age:18,
      sex:'boy'
    };
    

    约定属性值类型以及必要性

    Mycomponent.propTypes = {
       name : PropTypes.String.isRequired,
       age: PropTypes.number
     };
    
    • refs(尽量避免使用Refs)
      用户名<input type="text" ref={input => this.nameInput=input}/>
      const name = this.nameInput.value; //或得到input框的值
    

    相关文章

      网友评论

          本文标题:state,props,refs属性

          本文链接:https://www.haomeiwen.com/subject/yelrmftx.html