react-父子组件传值-父传子
1.父组件往子组件内传值,只需要通过子组件自定义属性的方式传入即可,在子组件中,直接使用this.props.属性名访问(ES6class形式的组件,function类的组件直接取参数props.属性名即可);‘
class Children extends React.Component {
constructor(props){
super(props);
this.state={
msg:'我是子组件'
}
}
render(){
return (
<div className='children'>
<h1>{this.state.msg}</h1>
<p>{this.props.pmsg}</p>
</div>
)
}
}
class Parent extends React.Component {
constructor(){
super();
this.state={
msg:'我是父组件内的数据'
}
}
render(){
return (
<div className='parent'>
<h1>我是父组件</h1>
<Children pmsg={this.state.msg} />
</div>
)
}
}
ReactDOM.render(
<Parent />,
document.getElementById('app')
)
2.父传孙,直接一层一层套即可,用法一样。