当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 基于react组件之间的参数传递(详解)

基于react组件之间的参数传递(详解)

2017年12月12日  | 移动技术网IT编程  | 我要评论

1、父组件向子组件传递参数

class child extends component {
    componentdidmount(){
      let name = this.props.default;
      console,log(name);
    }
    render(){
      const { default} = this.props;
      return (
        <input />
      )
   }
}
import react, { component } from 'react';
import child from './child';

class parent extends component {
  state = {
    name: 'bob'
  }
  render() {
    return (
      <div>
        <child default={this.state.name} />
      </div>
    )
  }
}

2、子组件向父组件传递参数

class child extends component {
    state={
      name:'bob'
    }
    componentdidmount(){
      this.props.toparent(this.state.name);
    }
    render(){
      return (
        <input />
      )
   }
}
import react, { component } from 'react';
import child from './child';

class parent extends component {
   state = {
    name:''
  }
  getchildinfo = (name)=>{
     this.setstate({name:name});
   }
  render() {
    return (
      <div>
        <child toparent={this.getchildinfo.bind(this)} />
      </div>
    )
  }
}

以上这篇基于react组件之间的参数传递(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网