基于react组件之间的参数传递(详解)
程序员文章站
2022-04-29 08:12:38
1、父组件向子组件传递参数
class child extends component {
componentdidmount(){
le...
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组件之间的参数传递(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。