父组件
import React, {Component} from 'react'
import Child from './Child'
export default class Parent extends Component {
constructor(props) {
super(props)
this.state = {
name: ''"
}
}
getChildName = (result, name) => {
this.setState({
childrenMsg: name
})
}
render() {
return (
<div>
<h2>子组件传来的值为:{ this.state.name }</h2>
<hr/>
<Child parent={ this } />
</div>
)
}
}
子组件
import React, {Component} from 'react'
export default class Child extends Component {
constructor(props) {
super(props)
this.state = {
name: '我是子组件',
}
}
toParent = () => {
// console.log(this.props.parent.getChildName.bind(this, this.state.name))
this.props.parent.getChildName(this, this.state.msg)
}
render() {
return (
<div>
<div onClick={ this.toParent }>点击传值</div>
</div>
)
}
}