react 子组件回调父组件函数

本文探讨了在React中如何通过props将父组件的函数传递给子组件,实现子组件调用父组件的方法。以一个具体的例子说明,子组件通过接收到的`changeState` prop来触发父组件的`goToBPMS`函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实就是父组件将父组件的函数(key:changeState,value:goToBPMS函数)传递给子组件,子组件从props中调用key为changeState的函数,父组件的goToBPMS函数就会调用。

子组件:

import React, { Component } from 'react';
import { Button } from 'antd';
import { connect } from 'dva';

let hide = false;
@connect(state => ({
  user: state.user,
}))
export default class BaseButton extends Component {
  state = {
    type: this.props.type,
    text: this.props.text,
  };
  componentDidMount() {
    xxxxx
  }
  
  // 回调父组件的函数去修改父组件的数据
  click() {
    this.props.changeState();
  }

  render() {
    return (
      <Button
        type="primary"
        htmlType="submit"
        style={{ height: '30px', width: '100px' }}
        onClick={this.click.bind(this)}
      >
        {this.state.text}
      </Button>
    );
  }
}

父组件使用子组件:

import React, { Component } from 'react';
import { connect } from 'dva';
import BaseButton from './component/BaseButton';
import { INTEGRATION_NEXT_BUT_TEXT } from '../../constants/project';


class ProjectCheck extends Component {
  state = {
   
  };

 

  componentDidMount() {
    this.getDetail();
  }
    // 父组件被子组件回调的函数在这里~~
  goToBPMS() {
   
  }

  // 获取页面底部button状态
  getFooterButton() {
    return (
      <Row>
        <Col span={2}>
          <BaseButton
            text={INTEGRATION_NEXT_BUT_TEXT.text}
            changeState={this.goToBPMS.bind(this)}
            extraInfo={{ disable: hideButton }}
          />
        </Col>
      </Row>
    );
  }

  /**
   * @Description:渲染页面
   */
  render() {
    
    return (
      <div>
        <Form>
          <FormItem
            {...styles.tailFormItemLayout}
          >
            {this.getFooterButton()}
          </FormItem>
        </Form>
      </div>
    );
  }
}

const ProjectCheckWrapper = Form.create()(ProjectCheck);
export default ProjectCheckWrapper;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值