export interface RChild{
input:()=>void
...
}
export interface TChild{
setIndex:(key)=>void
}
const mapStateToProps=({nav})=>({index:nav.index})
const mapDispathToProps={
setIndex:(key)=>({
type:'nav/setIndex',
payload:key
})
}
const child=forwardRef<RChild,TChild>({setIndex},ref}=>{
useImperativeHandle(ref,()=>({
input, //传给父类的方法或者值
})
return (<>.....</>)
})
export defualt child
//注意
//如果child组件需要是基于connect的话,父类可以获取不到子类方法,界面也可能警告attempts to access this ref
export defualt connect(mapStateToProps,mapDispathToProps,null,{forwardRef:true})(child)
const Parent =()=>{
const preRef=useRef<RChild>(null)
// 调用input方法时;
//preRef.current!.input()
return (<>
<Child ref={preRef}/>
</>)
}
export defualt Parent