美好的一天!MobX:分配後重新排列
我有一個父組件:
@observer
class ToDos extends Component {
componentWillMount() {
this.state = new State();
this.onClearAllCompleted = this.onClearAllCompletedHandler.bind(this);
}
onClearAllCompletedHandler() {
this.state.clearAllCompleted();
}
render() {
const todos = this.state.todos;
return (
<Provider todos={todos}>
{this.props.children}
<ClearAllButton onClick={this.onClearAllCompleted} />
</Provider>
);
}
}
和狀態類是:
class TodosState {
@observable todos = [
{ title: 'Sleep', completed: true },
{ title: 'Sleep more', completed: false },
{ title: 'Sleep more than before', completed: false }
];
@action
clearAllCompleted() {
this.todos = this.todos.filter(todo => !todo.completed);
}
}
當我嘗試清除所有已完成的待辦事項,它會清除他們在瀏覽器控制檯的警告:MobX Provider: Provided store 'todos' has changed. Please avoid replacing stores as the change might not propagate to all children
。
在此之後什麼也沒有發生:我已經老了渲染HTML;(
所以,我認爲兒童有一個對象,並在國家分配後的引用我有不同的參考待辦事項觀察的對象童車不知道。這一點,他們觀察到沒有改變過所以,我可以在這種情況下做