美文网首页
父子传参

父子传参

作者: 輪徊傷 | 来源:发表于2019-06-25 20:18 被阅读0次
image.png

在react中父子组件传参
一 、父传子

父传子图片

子组件把值传给父组件
在父组件中 其实可以把子组件里的方法用箭头函数,这样就不要绑定this了

export default class Son extends Component {
  static propTypes = {

  };
  
  gaibian=()=>{
    let aaa={name:"guining",age:22};
    console.log(this)
    this.props.change(aaa)
  }
image.png
image.png

在子组件中


****

代码如下

//父组件的代码
export class Text extends Component {
  static propTypes = {
    test: PropTypes.object.isRequired,
    actions: PropTypes.object.isRequired,
  };
  state={
    list:{name:'libin',age:18}
  }
  change=(zhi)=>{
    let {list} = this.state;
    if(zhi==={}){
      console.log('值不能为空')
    }
    this.setState({
      list:zhi
    })
  }

  render() {
    let {list} = this.state;
    return (
      <div className="test-text">
      父组件
        <Son List={list} change={this.change} ></Son>
      </div>
    );
  }
}
//子组件的代码
export default class Son extends Component {
  static propTypes = {
  };
  constructor(props){
    super(props);
    this.gaibian=this.gaibian.bind(this)
  }
  gaibian(){
    let aaa={name:"guining",age:22};
    console.log(this)
    this.props.change(aaa)
  }

  render() {
    let {List} = this.props;
    console.log(List)
    return (
      <div className="test-son">
       子组件
       <p>姓名:{List.name}</p>
       <p>年龄:{List.age}岁</p>
       <button onClick={this.gaibian}>改变</button>
      </div>
    );

相关文章

  • router-view 父子传参

    导语: 习惯了父子组件传参,今天聊一聊router-view传参。其实本质也是父子组件传参,方法一模一样,之前也提...

  • 父子传参

    在react中父子组件传参一 、父传子 子组件把值传给父组件在父组件中 其实可以把子组件里的方法用箭头函数,这样就...

  • 父子传参

  • 父子传参

    父组件 父组件向子组件传递数据 1.父组件绑定属性,给子组件传递数据2.子组件通过props接收父组件传递过来的数...

  • Flutter 父子组件传参 之 父组件向StatefulWid

    Flutter 父子组件传参 之 父组件向StatefulWidget有状态子组件传参https://www.we...

  • 父子传参(组件)

    第一种,父子组件通信 一.父组件向子组件传值 创建子组件,在src/components/文件夹下新建一个Chil...

  • vue 父子组件传参

    引用组件时,首先import引入,再注册,最后方可使用。同时注意,如果是驼峰式helloWorld ,在调用时就要...

  • react父子组件传参

    父子组件通信主要用到props,如下: 在父组件中: 在子组件中: 通过上面例子可以看出,在父组件中,我们引入子组...

  • vue 父子组件传参

  • vue父子组件传参

    1、父组件可以使用 props 把数据传给子组件。2、子组件可以使用 $emit 触发父组件的自定义事件。 vm....

网友评论

      本文标题:父子传参

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