react+ts 父子组件传值(类组件、函数组件)

2 篇文章 0 订阅

1.父组件为类组件

tips:classNavigateInterface是自己定义的interface 规定要接受什么值
// 引入interface
import {classNavigateInterface} from '@/assets/Interface'
// 引入react
import {Component,createRef} from "react";
// 引入弹窗Dialog
import ManifesInformationDialog from '@/components/dialog/ManifestInformation-dialog'

class SetOutFlight extends Component<classNavigateInterface>{
  ManifesInformationDialogRef:any
  constructor(props:classNavigateInterface){
    super(props)
    this.ManifesInformationDialogRef=createRef()
  }
  render(){
		return(
			<ManifesInformationDialog id='111' ref={this.ManifesInformationDialogRef}  handle={this.handleCallBack} />
		)
	}
}

2.子组件为函数组件

tips:子组件接受里面一定要写一个ref:any, 最后导出的时候要用forwardRef包裹我们的组件,在useImperativeHandle的方法就可以让我们的父组件调用了
// 引入react
mport { useState,forwardRef, useImperativeHandle } from 'react';
// 引入interface 规范传值
import {ManifesInformationDialogIprops} from '@/assets/Interface';
const ManifesInformationDialog = (props:ManifesInformationDialogIprops,ref:any) => {
  const [isModalVisible, setIsModalVisible] = useState(false);
  useImperativeHandle(ref, () => ({
    getValues: () => {
      return ('hell word')
    },
    // 打开弹窗
    openDialog:() => {
      setIsModalVisible(true)
    }
  }))
  // 确定
  const handleOk = () => {
    props.handle()
    setIsModalVisible(false)
  }
  return(
		<div className="child">我是子组件</div>
	)
}
export default forwardRef(ManifesInformationDialog)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值