0
如何對數組中的變化做出反應?不執行子陣列更改/刪除Mobx反應
var todos = mobx.observable([
{
title: "Make Pasta",
subtasks: [
{ title: 'Pasta' }, { title: 'Salt' },
]
},
{
title: "Make coffee",
subtasks: [
{ title: 'Hot Water' }, { title: 'Milk' },
]
}
]);
const reactionSubtask = mobx.reaction(
() => todos.map(todo => todo.subtasks),
subtasks => console.log("reaction subtasks >", subtasks)
);
const reactionTask = mobx.reaction(
() => todos.map(todo => todo.title),
titles => console.log("reaction title >", titles.join(', '))
);
const resultSubtaskRemove = todos[1].subtasks.remove(todos[1].subtasks[0]);
console.log('Subtask remove result >', resultSubtaskRemove);
const resultSubTaskAdd = todos[1].subtasks.push({title: 'Filter'});
console.log('Subtask add result >', resultSubTaskAdd);
const resultSubTaskChange = todos[1].subtasks[0].title = 'Change titled';
console.log('Subtask change result >', resultSubTaskChange);
<script src="https://unpkg.com/mobx/lib/mobx.umd.js"></script>
反應。
在我的情況下,我使用mobx反應和我的React組件不更新時,我更改兒童數組。