reacthooks使⽤ref操作form⼦组件
⽗组件
import React, { useState, useRef, useEffect, forwardRef } from 'react'
import ModelForm from './ModelFormCopy'
这样写 const ForwardEditInfo = forwardRef(ModelForm)) 如果组件中有 connect 要使⽤ forwardRef 就会报错
Warning: forwardRef requires a render function but received a `memo` component. Instead of forwardRef(memo(...)), use memo(forwardRef(...)).改⽤下⾯这个
注意:!!这⾥不在Component上使⽤ref 换个属性名不然会导致覆盖
const ForwardEditInfo = (forwardRef((props, ref) => <ModelForm {...props} refInstance={ref} />))
const DrawerEdit = (props)=>{
let addRef = useRef()
const submit=()=>{
// 获取到了
let params = addRef.current.p()
}
return(
<ForwardEditInfo
ref={addRef}
searchMobile={searchMobile}
reacthooks理解initialValues={currentItem}
></ForwardEditInfo>
)
}
⼦组件
const ModelForm=(props)=>{
const editRef = useRef()
fInstance, () => ({
p: FieldsValue
}))
return (
<div ref={editRef}>
</div>
)
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论