react子组件向父组件传递数据和父组件向子组件传递数据的正确写法和函数互相调用

存在伪代码,自行解决

父组件 App:

import React, { useState, useEffect } from 'react';
import { Col, Row, Space, Table, Switch, Tag, Button } from 'antd';
import Qs from 'qs';

const App: React.FC = () => {
  //父组件传数据到子组件函数
  const [opne, setOpen] = useState<boolean>(false);
  const tj1 = () => {
         setOpen(true)
         console.log("添加",opne);
  }
  // 子组件向父组件传递数据的回调函数
  const handleChildDataChange = (data: string) => {
    console.log('子组件传递的数据:', data);
  };

  return (
    <>
      {/* 父组件内容 */}
      <Button onClick={tj1 }>Send Data to Parent</Button>

      {/* 将回调函数传递给子组件 */}
      <USERTJ message={opne} onChildDataChange={handleChildDataChange} />
    </>
  );
};
export default App;

子组件 USERTJ:

import React, { useState } from 'react';
import { Button, Modal } from 'antd';
//定义类型
interface ChildProps {
  message: boolean,
  onChildDataChange: (data: string) => void;
}

const USERTJ: React.FC<ChildProps> = ({ message, onChildDataChange }) => {
  console.log('来自父组件来的数据', message);
  
  // 子组件向父组件传递数据
  const handleInputChange = () => {
        onChildDataChange('Data from child component');
  };

  return (
    <>
        {/* 子组件内容 */}
        <Button onClick={handleInputChange}>Send Data</Button>
    </>
  );
};

export default USERTJ;

子组件和父组件的函数互相调用

父组件:

import React, { useRef, useState, useEffect } from 'react';
import { Col, Row, Space, Table, Switch, Tag, Button } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { getDateFormatt } from "@/util/date"

import Qs from 'qs'
//查询用户列表
import { SysUser } from "@/request/api"
import "./user.less"
import USERTJ, { ChildComponentRef } from "@/components/user"

const App: React.FC = () => {
    const childRef = useRef<ChildComponentRef>(null);
    // 子组件向父组件传递数据的回调函数
    const handleChildDataChange = (data: string) => {
        console.log('子组件传递的数据:', data);
    };
    //调用子组件的函数
 const tychildFunction= (data: string) => {
        if (childRef.current) {
            childRef.current.childFunction(); // 调用子组件的函数
        }
    };
    return (
        <>
         <Button type="primary" onClick={tychildFunction}>
                    调用子组件
            </Button>
            {/* 弹窗 */}
            {/* 将回调函数传递给子组件 */}
            <USERTJ ref={childRef} onChildDataChange={handleChildDataChange} />
        </>
    )

};
export default App;

子组件

import React, { useState, forwardRef, useImperativeHandle, ForwardedRef } from 'react';
import { Button, Modal } from 'antd';
interface ChildProps {
    // 定义子组件的其他属性
    onChildDataChange: (data: string) => void;

}
export interface ChildComponentRef {
    childFunction: () => void;
}

const USERTJ = forwardRef<ChildComponentRef, ChildProps>((props, ref: ForwardedRef<ChildComponentRef> ) => {
    //从props取出父组件的onChildDataChange 函数,让子组件调用
    const { onChildDataChange } = props;
    const handleInputChange = () => {
        // 子组件向父组件传递数据
        onChildDataChange('Data from child component');
    };
    const childFunction = () => {
        console.log('子组件函数被调用');
    };

    // 将子组件的函数暴露给父组件通过 ref 获取
    useImperativeHandle(ref, () => ({
        childFunction,
    }));

    return (
        <>
                <Button type="primary" onClick={handleInputChange}>
                    调用父组件
            </Button>
        </>
    );
});
export default USERTJ;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值