Is it possible to use ES6's Set
data structure in React?
For example, if I have a checklist composed of distinct items, and I'd like to maintain each item's checked state. I'd like to write something like this:
export default class Checklist extends React.Component {
constructor(props) {
super(props);
this.state = {
checkedItems: new Set()
}
}
addItem(item) {
//...?
}
removeItem(item) {
//...?
}
getItemCheckedStatus(item) {
return this.state.checkedItems.has(item);
}
// More code...
}
I understand there may be a problem with the fact that a Set is mutable by nature, and React performs a shallow comparison when updating the component, so it expects immutable objects to be passed and held in the state. However, is there a way to hold and maintain a Set object in the state?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…