# 如何使用 react hooks 实现一个计数器的组件

更多描述

如何使用 react hooks 实现最简单一个计数器的组件

为了保证最最简单化,不需要暂停与开始状态

Issue

欢迎在 Issue 中交流与讨论: Issue 15 (opens new window)

Author

回答者: mexyx (opens new window)

import React, { useEffect, useState } from "react";

function App() {
    const [count, setCount] = useState(0);
    useEffect(() => {
        const timer = setInterval(() => {
            setCount(count => count + 1)
        }, 1000)

        return () => {
           clearInterval(timer)
        }
    }, [])

    return <h1>{count}</h1>
}
Last Updated: 1/15/2021, 6:07:33 AM