useImperativeHandle 这个hook传递的ref无法引用到函数式组件实例,无法调用函数式组件暴露的接口
giligiliduang opened this issue · comments
giligiliduang commented
const Lorry = forwardRef((props, ref) => {
const startLorry = () => {
console.log('start')
}
const stopLorry = () => {
console.log('stop')
}
useImperativeHandle(
ref,
() =>({
startLorry,
stopLorry
})
)
return (
<>
('')
</>
)
})
function main(){
let lorryRef=useRef(null)
useEffect(()=>{
lorryRef.current.startLorry() //报错,因为lorryRef.current为null
},[])
<Lorry ref={lorryRef}></Lorry>
}
代码如上,这个问题在React里面是不存在的
司徒正美 commented
已经修复