【发布时间】:2020-05-23 11:15:50
【问题描述】:
我正在尝试学习 React 钩子,并且正在尝试编写一个增加计数状态的简单函数。
import React, { useState } from "react";
export const HookCounter = () => {
const [count, setCount] = useState(0);
const incrementCount = (count) => {
setCount(count + 1);
};
return (
<div>
<button onClick={incrementCount}>Press me!</button>
<h1>
You have pressed the button <strong>{count}</strong> times
</h1>
</div>
);
};
但是,当我单击按钮时。而不是像我希望的那样增加计数器。我看到的是:
You have pressed the button [object Object]1 times.
这是为什么?
【问题讨论】:
标签: reactjs react-hooks use-state