react 定时器

    0
  1. 方案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>
  );
}

  1. 方案2
  1. 方案3
useInterval

#讨论区
00条评论
实时对话
loading...