父组件与子组件间的通信
子组件获取父组件的信息
父组件:
<div className="father">
<Child age={this.state.age} name={this.state.name}></Child>
</div>
子组件:
出入的值存在this.props中
this.props.age
this.props.name
父组件获取子组件的信息
1、回调函数的方式
子组件:
可直接调用子组件的props
this.props.setName(name)
父组件:
getName=(name){
this.setState({name})
}
<div className="father">
<Child setName={this.getName}></Child>
</div>