React函数式组件的父子方法互相调用,父组件调用子组件方法,子组件调用父组件方法

函数式组件的父子组件方法互相调用

编写子组件 Child

注意:子组件需要使用 forwardRef 函数包裹,然后使用 useImperativeHandle 暴露属性和方法

import React, {forwardRef, useImperativeHandle} from 'react';
import {Button} from 'antd';

// 子组件使用forwardRef函数包裹
// forwardRef函数接收两个参数,第一个参数是props,第二个参数是ref
const Child = forwardRef((props, ref) => {
  // 定义方法,将来由父组件调用
  const getChildStr = () => {
    console.log('子组件的getChildStr方法被触发');
    return '来自子组件的返回值';
  };

  const getParentFn = () => {
    // 调用父组件的方法
    props?.parentAddCount?.();
  };

  // useImperativeHandle函数接收两个参数,第一个参数是ref,第二个参数是一个函数
  // 这个函数返回一个对象,这个对象中的属性和方法会被暴露给父组件
  useImperativeHandle(ref, () => {
    return {
      getChildStr,
    };
  });

  return (
    <div className={'p-3 bg-amber-500'}>
      <div>我是子组件</div>

      <Button onClick={getParentFn}>调用父组件方法</Button>
    </div>
  );
});

export default Child;

编写父组件

import React, { useRef } from 'react';
import Child from '@/pages/test/Child';
import { Button } from 'antd';

const Index = () => {
  let [count, setCount] = React.useState(0);
  let childRef = useRef();

  // 提供给子组件调用的方法,子组件使用 prop.parentAddCount() 实现调用父组件的方法
  const addCount = () => {
    setCount(count + 1);
  };

  // 调用子组件方法,使用 childRef.current 获取子组件暴露的属性和方法
  const getChildStr = () => {
    let childStr = childRef.current?.getChildStr();
    console.log(childStr);
  };

  return (
    <>
      <Child ref={childRef} parentAddCount={addCount} />

      <div style={{ marginTop: 20 }}>count:{count}</div>
      <Button onClick={getChildStr}>调用子组件的方法</Button>
    </>
  );
};

export default Index;

效果展示

image-20240518215529255

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React中,组件可以通过props来调用组件中的方法。有几种常见的方法可以实现这个功能。 第一种方法是在组件中定义方法,并将其绑定到组件上。在组件中,可以通过this.props来调用组件中的方法。例如,在组件中定义一个方法fun,并将其绑定到组件上,然后在组件中通过this.props.getFun()来调用方法。\[1\] 第二种方法是在组件中通过this.props来直接调用组件中的方法。在组件中,可以通过this.props来访问组件传递过来的方法,并直接调用它。例如,在组件中通过this.props.getFun()来调用组件中的方法。\[2\] 第三种方法是在组件页面初始化完成后,通过this.props调用组件页面的方法,并将组件页面内容绑定到组件。在组件的componentDidMount()生命周期方法中,可以通过this.props.onRef(this)来调用组件中的方法,并将组件的内容绑定到组件。\[3\] 以上是React组件调用组件方法的几种常见方法。具体使用哪种方法取决于你的需求和项目的架构。 #### 引用[.reference_title] - *1* *2* [react组件调用组件方法](https://blog.csdn.net/weixin_30416871/article/details/94885711)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [React父子组件方法调用](https://blog.csdn.net/noeal/article/details/115300055)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值