如果我错了,请纠正我,但目前使用替换是不可能的,因为替换会替换整个可观察数组,而应该使用映射?
我有一个像这样的可观察数组:
@observable questionsList = [];
在服务器调用时,它会填充 2 个对象,每个对象都有一个不同的id
字段,所以它看起来像这样:
@observable questionsList = [
{id: 1,
question: "Is the earth flats?",
answer: "Some long answer here..."
{id: 2,
question: "Does the moon have life?"}
answer: "Some long answer here..."}
];
所以 id of 的问题1
有一个需要修复的错字,应该是Is the earth flat?
鉴于以下情况:
const newQuestionId = 1;
const newQuestion = "Is the earth flat?"
const oldQuestion = this.questionsList.find(question => question.id === newQuestionId);
oldQuestion.replace(newQuestion) // Would be nice if this syntax worked
现在我有正确的oldQuestion
但如何 questionList
用新问题替换数组中的它?我试过更换,但没有奏效。地图是唯一的方法吗?如果是这样,我不确定如何让 map 工作,因为我只使用可观察数组。
如何使用 MobX 完成此任务?