账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    子组件怎么向父组件传值呢 reactjs
    44
    0

    父组件向子组件传输用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;
    
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部