【发布时间】:2018-09-05 17:18:17
【问题描述】:
我实际上是在尝试在用户单击时切换元素上的类。但不幸的是,我的代码只为单个元素设置了类。看起来视图没有为后续点击刷新,即使设置的类也没有删除。但我的商店正在正常更新。
这是我的代码。
class MyInterests extends Component {
constructor(props) {
super(props);
this.state = {selected: []};
}
toggleChip(id, index, event){
const { interestChanged } = this.props;
let index = this.state.selected.indexOf(id);
if(index === -1){
this.state.selected.push(id);
}else{
this.state.selected.splice(index, 1);
}
interestChanged(this.state.selected);
}
render() {
const {classes, myinterests: { categories, userinterest } } = this.props;
const getClassNames = (id) => {
return classNames(classes.chip, {[classes.selected]: (userinterest.indexOf(id) !== -1)});
}
return (
/*..... Few element to support styles....*/
{categories.map((data, index) => {
return (
<Chip
key={data._id.$oid}
label={data.name}
onClick={this.toggleChip.bind(this, data._id.$oid, index)}
className={getClassNames(data._id.$oid)}
/>
);
})}
);
}
}
谁能告诉我这有什么问题或者我怎样才能做到这一点?
【问题讨论】:
标签: css reactjs react-native react-redux material-ui