react父组件和子组件之间相互调用方法

父组件:

import React, { Component } from 'react';
import Child from './child2'
class Parents extends Component {

  constructor(props) {
    super(props);
  }


  handleCancel = () => {
    console.log('父组件的方法被子组件调用了');
  }

  childClick = () => {
    this.child.onShow()
  }

  render() {
    return (
      <section>
        <button onClick={this.childClick}>父组件调用子组件的函数</button>
        <Child handleCancel={this.handleCancel} onRef={(ref) => { this.child = ref }}></Child>
      </section>
    );
  }

}

export default Parents;

 

子组件:

import React, { Component } from 'react';

class Child extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    this.props.onRef(this)
  }


  onShow() {
    console.log('子组件的方法被父组件调用')
  }

  render() {
    return (
      <section>
        <button onClick={() => { this.props.handleCancel() }}>子组件用this.props调用父组件的函数</button>
      </section>
    );
  }
}

export default Child;

 

React组件调用组件方法可以通过以下两种方式实现: 1. 使用类组件:在组件创建一个对子组件实例的引用,并将其传递给子组件的props。子组件在完成挂载时,通过调用组件传递的方法,并将当前子组件的实例作为参数传递给该方法。这样,组件就可以通过子组件的实例来调用组件方法。 ```jsx // Parent.js class Parent extends Component { // ... handleChildEvent = (ref) => { this.childRef = ref; // 将子组件的实例存储到this.childRef } handleClick = () => { this.childRef.sendMessage(); // 通过子组件的实例调用组件方法 } render () { return ( <> <Child onChildEvent={this.handleChildEvent} /> <button onClick={this.handleClick}>Trigger Child Event</button> </> ); } } ``` ```jsx // Child.js class Child extends Component { componentDidMount () { this.props.onChildEvent(this); // 在子组件调用组件传递的方法,并将当前子组件的实例作为参数传递给该方法 } sendMessage = () => { console.log('sending message'); } render () { return ( <span>Child</span> ); } } ``` 2. 使用函数组件React Hook:在子组件使用`useImperativeHandle` Hook,将子组件方法暴露给组件组件通过创建一个ref,并将其传递给子组件的`onRef` prop,从而获取子组件的实例,并通过该实例调用组件方法。 ```jsx // Parent.js import { useRef } from 'react'; function Parent() { const childRef = useRef(null); const handleChildEvent = (ref) => { childRef.current = ref; // 将子组件的实例存储到childRef } const handleClick = () => { childRef.current.sendMessage(); // 通过子组件的实例调用组件方法 } return ( <> <Child onRef={handleChildEvent} /> <button onClick={handleClick}>Trigger Child Event</button> </> ); } ``` ```jsx // Child.js import { useImperativeHandle } from 'react'; function Child(props, ref) { const sendMessage = () => { console.log('sending message'); } useImperativeHandle(props.onRef, () => ({ sendMessage })); return <span>Child</span>; } export default React.forwardRef(Child); ``` 以上是两种常用的在React实现组件调用组件方法的方式。第一种是使用类组件,通过引用传递子组件实例的方式进行通信。第二种是使用函数组件React Hook,在函数组件使用`useImperativeHandle` Hook来暴露子组件方法组件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [React 组件调用组件方法](https://blog.csdn.net/qq_40738077/article/details/119427313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值