Created
July 5, 2023 20:40
-
-
Save ernestlv/8435ffe9de9b80a53b84e63beb34e8a0 to your computer and use it in GitHub Desktop.
#React - Access DOM element using Hook Ref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Square = () => { // Root Functional Component | |
let inputRef = React.useRef(null); // React Hook | |
const click = () => { // 'this' points to window | |
alert(inputRef.current.value); | |
} | |
console.log("render square"); | |
// View | |
return ( | |
<div style={{color:'white'}}> | |
<input ref={inputRef} /> | |
<button onClick={click}>show</button> | |
</div> | |
); | |
} | |
const root = ReactDOM.createRoot(document.querySelector("#app")); | |
root.render(<Square />); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment