【发布时间】:2020-07-06 11:28:24
【问题描述】:
*更新
我的状态没有更新任何按钮。 如何将 button.js 上按下的值作为 handleChange 的条件传递?
- 希望在单击按钮组件时使用回调更新状态值
- 在另一个组件中显示结果
- 当我按下按钮时,我的状态没有更新
App.js
const [neutral, setNeutral] = useState(0);
const [bad, setBad] = useState(0);
// const [all, setAll] = useState(0);
const handleChange = (value) => {
// const data = ["good", "bad", "neutral"];
if (value === "good") {
setGood(good + 1);
console.log(value);
} else if (value === "neutral") {
setNeutral(neutral + 1);
} else if (value === "bad") {
setBad(bad + 1);
}
};
Return (
<Container className="Container">
<h2>Give Feedback</h2>
<Button onClick={handleChange} />
{/* statictics */}
<Statistics />
</Container>
);
}
Button.js
export default function Button(props) {
return (
<div>
<button value="good" onChange={props.handleChange}>
Good
</button>
<button value="neutral" onClick={props.handleChange}>
Neutral
</button>
<button value="bad" onClick={props.handleChange}>
Bad
</button>
</div>
);
}
收到新错误,建议我添加生命周期方法。 我不知道这是否矫枉过正,以及是否有更简单的方法来实现此计数解决方案。
【问题讨论】:
标签: reactjs react-hooks react-props react-component