0
点赞
收藏
分享

微信扫一扫

Python代码重构库之rope使用详解

phpworkerman 2024-02-03 阅读 8

react.forwardRef使用ref暴露DOM节点给父组件

1.使用场景

在这里插入图片描述

import { forwardRef, useRef } from "react"

// 子组件
// function Son () {
//   return <input type="text" />
// }

const Son = forwardRef((props, ref) => {
  return <input type="text" ref={ref} />
})


// 父组件
function App () {
  const sonRef = useRef(null)
  const showRef = () => {
    console.log(sonRef)
    sonRef.current.focus()
  }
  return (
    <>
      <Son ref={sonRef} />
      <button onClick={showRef}>focus</button>
    </>
  )
}

export default App
举报

相关推荐

0 条评论