useRef
function App() {
const inputRef = useRef(null);
const onClickInputFocus = () => {
inputRef.current.focus();
};
return (
<div className="App">
<input type="text" ref={inputRef} />
<button onClick={onClickInputFocus}>input 포커스 하기</button>
</div>
);
}function App() {
const [count, setCount] = useState(0);
const intervalID = useRef(0);
useEffect(() => {
intervalID.current = setInterval(() => {
setCount((count) => {
return count + 1;
});
}, 1000);
}, []);
const onClickStop = () => {
clearInterval(intervalID.current);
};
return (
<div className="App">
<div>{count}</div>
<button onClick={onClickStop}>Stop</button>
</div>
);
}
Last updated