RubyLouvre / anu

the React16-compat library with hooks

Home Page:https://rubylouvre.github.io/anu/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

giligiliduang opened this issue · comments

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里面是不存在的

已经修复