Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Created July 5, 2023 20:40
Show Gist options
  • Save ernestlv/8435ffe9de9b80a53b84e63beb34e8a0 to your computer and use it in GitHub Desktop.
Save ernestlv/8435ffe9de9b80a53b84e63beb34e8a0 to your computer and use it in GitHub Desktop.
#React - Access DOM element using Hook Ref
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