我是 React JS 的新手。我有多个图标按钮。OnClick 我只想让单击的按钮更改其颜色。我使用了状态,但是当状态改变时,所有按钮的颜色都会改变。我应该采用什么方法?有没有一种方法可以在不使用状态的情况下改变颜色?需要钥匙或身份证吗?我提供的代码已被裁剪(意味着它仅包含我认为相关的部分)。
class Utilitybar extends React.Component {
constructor(props) {
super(props);
this.state = {
bgColor: "default"
};
}
render() {
return (
<div>
<IconButton color={
this.state.bgColor
}
onClick={
() => {
this.props.vidToggle();
if (this.state.bgColor === "default") {
this.setState({ bgColor: "primary" })
} else {
this.setState({ bgColor: "default" })
}
}
}>
<FaPlayCircle />
</IconButton>
<IconButton color={
this.state.bgColor
}
onClick={
() => {
this.props.fileToggle();
if (this.state.bgColor === "default") {
this.setState({ bgColor: "primary" })
} else {
this.setState({ bgColor: "default" …Run Code Online (Sandbox Code Playgroud)