Hiểu về hook useRef của React như thế nào cho đúng
Bài viết được sự cho phép của tác giả Lưu Bình An
const [value, setValue] = React.useState("init value");function usePersistentValue(initValue) {
return React.useState({
current: initialValue,
})[0];
}function Counter() {
const [count, setCount] = React.useState(0);
let id;
const clear = () => {
window.clearInterval(id);
};
React.useEffect(() => {
id = window.setInterval(() => {
setCount((c) => c + 1);
}, 1000);
return clear;
}, []);
return (
<div>
<h1>{count}</h1>
<button onClick={clear}>Stop</button>
</div>
);
}Last updated