zoukankan      html  css  js  c++  java
  • react状态提升问题::::

    父组件传值给子组件,只需要在组件上写上naverightstates={this.state.naverightstates},然后在子组件里面引用this.props.naverightstates.
    如果想通过子组件向父组件传值,再把值传给兄弟组件,可以把函数和变量写在父组件中
    constructor(props){
    super(props);
    this.state={
    naveleftstates:true,
    naverightstates:false,
    addlist:true
    }
    }

    naveleftclick1(){
    this.setState({
    naveleftstates:true,
    naverightstates:false,

    })
    }

    naverightclick2(){
    this.setState({
    naveleftstates:false,
    naverightstates:true,

    })
    }

    然后通过callback1={this.naveleftclick1.bind(this)} callback2={this.naverightclick2.bind(this)}的方法传递到子组件中。
    并在子组件中通过回调函数的方式引用,不能直接引用,否者会报错
    正确方法:

    aaa(){
    this.props.callback1();
    }
    onClick={this.aaa.bind(this)} ,

    错误方法:onClick={this.props.callback1()}

  • 相关阅读:
    vue中computed和watch的区别,以及适用场景
    vue中使用过的全局API
    厦门中控
    设置圆角的弧度,保持兼容性
    伪元素::after和::before
    SpringMVC
    mui问题
    错误记录
    Android错误
    Android之界面(布局文件layput)
  • 原文地址:https://www.cnblogs.com/dongkx/p/7156526.html
Copyright © 2011-2022 走看看