UseRef — A Side Note to the Official Doc

The following can be read as a side note to the official React Doc

Image for post
Image for post
meme reference

UseRef

function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
Image for post
Image for post
example(1): useRef inside useEffect
Image for post
Image for post
example(2) useRef called in event handler
Image for post
Image for post
example(2) useRef can hold a function

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store