Javascript 如何在React Native with hook中使用refs?

Javascript 如何在React Native with hook中使用refs?,javascript,react-native,react-hooks,ref,Javascript,React Native,React Hooks,Ref,我正在开发一个带有挂钩的React原生应用程序。(不上课)。当我们使用类时,我们可以像这样创建对子组件的引用 <Child ref={component => this.childComponent = component} /> export default function Child() { const foo = () => { //do something } return( //something ) } export

我正在开发一个带有挂钩的React原生应用程序。(不上课)。当我们使用类时,我们可以像这样创建对子组件的引用

<Child ref={component => this.childComponent = component} />
export default function Child() {
  const foo = () => {
    //do something
  }

  return(
    //something
  )
}


export default function Parent() {
  const myFunc = () => {
    ref.foo();
  }

  return(
    <Child ref={.........} />  //This is what I want to know how to do?
  )
}
this.childComponent=component}/>
但是,当我们使用钩子时,如何做到这一点呢

我想要这样的东西

<Child ref={component => this.childComponent = component} />
export default function Child() {
  const foo = () => {
    //do something
  }

  return(
    //something
  )
}


export default function Parent() {
  const myFunc = () => {
    ref.foo();
  }

  return(
    <Child ref={.........} />  //This is what I want to know how to do?
  )
}
导出默认函数子函数(){
常量foo=()=>{
//做点什么
}
返回(
//某物
)
}
导出默认函数父函数(){
常量myFunc=()=>{
参考foo();
}
返回(
//这就是我想知道的怎么做?
)
}

我希望您理解我的意思。

为了用钩子定义ref,您需要使用钩子。为了将ref应用于功能组件,您需要使用

函数子项(道具,参考){
常量foo=()=>{
//做点什么
}
使用命令式句柄(参考,()=>({
福
}));
返回(
//某物
)
}
导出默认React.forwardRef(子项)
导出默认函数父函数(){
const childRef=useRef(空)
常量myFunc=()=>{
childRef.current.foo();
}
返回(
)
}