react 传值方式

react 组件传值

1、分父亲向儿子传值

2、子向父亲传值

3、没有嵌套关系的组件之间传值,例如兄弟组件传值


1.父组件向子组件传值  
(通过props来传值,这种应用,很多时候我们某个组件在不同的地方用到,但是就只是内容不一样,这样在调用组件就是父组件的时候给各自自己的值就好)
//子组件
class Es6cComponent extends React.Component{
constructor(props){
super(props);
}
render(){
return(
<div>
<div>{this.props.nameall}</div>
</div>
)
}
}
//父组件
class App extends React.Component{
render(){
return(
<div>
<Es6cComponent nameall="abc"/>
</div>
)
}
}

2.子组件给父组件传值 ( 回调函数)

3.兄弟组件传值(子组件传给父组件,由父组件再传给另外一个子组件)

猜你喜欢

转载自blog.csdn.net/amyloverice/article/details/80728983