【发布时间】:2019-11-28 02:12:40
【问题描述】:
我是 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" })
}
}
}>
<FaRegFileAlt />
</IconButton>
</div>
);
}
}
我希望只有点击按钮才能改变颜色。但显然它们都使用相同的状态,当状态改变时,所有按钮的颜色都会改变。
【问题讨论】:
标签: reactjs material-ui react-component