RubyLouvre / anu

the React16-compat library with hooks
https://rubylouvre.github.io/anu/
Apache License 2.0
3.18k stars 319 forks source link

useImperativeHandle 这个hook传递的ref无法引用到函数式组件实例,无法调用函数式组件暴露的接口 #1146

Closed giligiliduang closed 5 years ago

giligiliduang commented 5 years ago
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里面是不存在的

RubyLouvre commented 5 years ago

已经修复