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

阅读 8

2024-02-03

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)

0 0 举报