- 方案1
import { useEffect, useState } from "react";
export default function App() {
const [count, setCount] = useState(10);
useEffect(() => {
setTimeout(() => {
setCount(count - 1);
}, 1000);
}, [count]);
return (
<div className="App">
<h1>{count}</h1>
<button onClick={() => setCount((c) => c - 1)}>
Edit to see some magic happen!
</button>
</div>
);
}
- 方案2
- 方案3
useInterval