react.js 父组件ajax获取数据,通过props传递到子组件,子组件应该怎么获取值
展开全部
比如如下代码:
父组件岩碰:
class Father extends Component {
construtor(props){
super(props);
this.state={
name: 'Peter',
age: '26'
}
}
onChangeState(stateName){
this.setState(stateName)
}
render(){
<p>姓名:{this.state.name}</p>
<p>年龄:{this.state.age}</p>
<Child onClicked={this.onChangeState.bind(this)}/>
}
}
子组庆启件:
class Child extends Component {
render(){
<Button onClicked={()=>誉枣如this.props.onClicked({name: 'John'})}/>
}
}
父组件岩碰:
class Father extends Component {
construtor(props){
super(props);
this.state={
name: 'Peter',
age: '26'
}
}
onChangeState(stateName){
this.setState(stateName)
}
render(){
<p>姓名:{this.state.name}</p>
<p>年龄:{this.state.age}</p>
<Child onClicked={this.onChangeState.bind(this)}/>
}
}
子组庆启件:
class Child extends Component {
render(){
<Button onClicked={()=>誉枣如this.props.onClicked({name: 'John'})}/>
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询