像使用 forwardRef實現(xiàn)父調子組件的方法,后來發(fā)現(xiàn)如果同時使用connect 烫止,是調用不到
解決方法:connect 參數(shù)加上{forwardRef:true}
import React ,{useRef} from 'react'
import Son from './Son.js'
const father =()=>{
const ref = useRef()
return(<div onClick={()=>{
ref.current.test()
}}>{‘父調子方法’}
<Son ref={ref}></Son>
</div>)
}
export default father
import React ,{forwardRef,useImperativeHandle} from 'react'
import { connect } from "umi";
const Son =forwardRef((props,ref)=>{
useImperativeHandle(ref, () => {
return {
test: test,
}
})
})
const test =()=>{
console.log('11111111111')
}
export default connect(()=>({
}),null,null,{forwardRef:true}) (son)
*** 原因:ref被Hoc 高階組件{connect} 阻斷了***