【发布时间】:2021-06-22 21:58:04
【问题描述】:
当我单击 3 个项目中的任何一个时,只会增加最后一个项目。当对 Item 组件使用基于类的组件而不是功能组件时,一切都很好。有人可以解释一下吗?
const { Fragment, useState } = React;
const List = () => {
return (
<Fragment>
<Item /><Item /><Item />
</Fragment>
)
};
const Item = () => {
[count, setCount] = useState(0);
return (
<div className="item" onClick={() => setCount(count + 1)}>{count}</div>
);
};
ReactDOM.render(
<List />,
document.getElementById('root')
);
【问题讨论】:
标签: reactjs react-hooks