我刚刚开始使用Mobx,我正在尝试添加一个从可观察数组中删除项目的操作 . 我究竟做错了什么?
这是商店:
class ToDoStore {
@observable items = [];
@action addItem = (item) => {
this.items.push(item)
}
@action removeItem = (index) => {
this.items.splice(index, 1)
}
@computed get itemCount(){
return this.items.length;
}
}
这是App.js:
@inject('ToDoStore')
@observer class App extends Component {
handleRemove = (index) => {
this.props.ToDoStore.removeItem(index);
}
render() {
const {ToDoStore} = this.props;
return (
<div className="App">
{ToDoStore.items.map((item, index )=> <li key={index}>{item}<button onClick={(index) => this.handleRemove(index)}>REMOVE</button></li>)}
</div>
);
}
}
export default App;
这是我点击“删除”按钮时出现的错误
1 回答
将onClick处理程序更改为:
你把事件对象作为索引传递!