在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 子組件怎么向父組件傳值呢 reactjs

子組件怎么向父組件傳值呢 reactjs

父組件向子組件傳輸用prop,那如果子組件向父組件傳輸怎么傳呢
子組件通過click事件點(diǎn)擊,把回調(diào)里面的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

關(guān)于redux使用 可以看看我的個(gè)人博客 http://www.liuweibo.cn/blog

2017年2月19日 12:03
編輯回答
來守候

傳回調(diào)函數(shù)。
在父組件里寫個(gè)函數(shù)change(){}
然后通過props傳給子組件,再在子組件的click函數(shù)里調(diào)用this.props.change()
這種東西react的官方文檔里都有。

2017年3月8日 02:37