父组件向子组件传输用prop,那如果子组件向父组件传输怎么传呢
子组件通过click事件点击,把回调里面的flag变量里面值想传给父组件,怎么传呢
class Child_1 extends React.Component {//子组件
constructor(props) {
super(props);
this.state = {
getName: this.props.name,
getAge: this.props.age,
}
}
click = () => {
let flag = 'test';
}
render() {
const {getName, getAge} = this.state;
return (
<div>
<p>{`姓名:${getName}`}</p>
<p>{`年龄:${getAge}`}</p>
<div onClick={this.click}>click me</div>
</div>
)
}
}
export default Child_1;
import Child_1 from './Child_1';
class Parent extends React.Component {//父组件
constructor(props) {
super(props);
this.state = {}
}
componentDidMount() {
}
render() {
return (
<div>
<Child_1 name="张三" age="25"/>
</div>
)
}
}
export default Parent;
一 父子组件传值,props
1.父组件
onChange(用来接收子组件传的值){
}
<Child_1 name="张三" age="25" onChange={this.onChange.bind(this)} />
2.子组件
this.props.onChange(传递的值)
二 redux
关于redux使用 可以看看我的个人博客 http://www.liuweibo.cn/blog