父组件拿到子组件的值:使用forwardRef
import { forwardRef,useRef } from 'react';
const Input = forwardRef((props,ref)=>{
return <input type="text" ref={ref} />
})
const App = () => {
const inputRef = useRef(null);
const showRef = () => {
console.log(inputRef.current.value);
}
return (
<div className="home">
<Input ref={inputRef} />
<button onClick={showRef}>拿到子组件的值</button>
</div>
)
}
export default App