我使用mobx作为我的react本地应用程序的状态管理,我正在修改一个简单的I数组,如下所示:
let copyy = userStore.unreadChatIds;
copyy.push(e.message.chat_id);
userStore.setUnreadChatIds(copyy);不管我收到这个mobx警告,我不知道我为什么要得到它,因为我在我的mobx商店使用makeAutoObservable!
[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed. Tried to modify: UserStore@1.unreadChatIds我的商店
export class UserStore
{
constructor()
{
makeAutoObservable(this);
unreadChatIds=[];
setUnreadChatIds(payload)
{
this.unreadChatIds = payload;
}
}为什么我要得到这个错误,我如何解决它?如果使用makeAutoObservable并使用setter方法作为操作,则不会直接更改mobx状态。
发布于 2022-07-25 05:51:07
此警告发生在您直接修改userStore.unreadChatIds时。
要解决这个问题,您应该通过操作修改该值。
userStore.addChatId(e.message.chat_id);商店
export class UserStore
{
constructor()
{
makeAutoObservable(this, {
unreadChatIds: observable,
setUnreadChatIds: action,
addChatId: action,
});
unreadChatIds=[];
setUnreadChatIds(payload)
{
this.unreadChatIds = payload;
}
addChatId(id)
{
this.unreadChatIds.push(id);
}
}https://stackoverflow.com/questions/72080064
复制相似问题