简体   繁体   English

React Native mobx:由于启用了严格模式,因此不允许在不使用操作的情况下更改(观察到的)可观察值

[英]React Native mobx: Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed

I'm using mobx as state management for my react-native app, I'm modifying a simple array of ids like this:我正在使用 mobx 作为 state 管理我的 react-native 应用程序,我正在修改一个简单的 id 数组,如下所示:

let copyy = userStore.unreadChatIds;
copyy.push(e.message.chat_id);
userStore.setUnreadChatIds(copyy);

However I'm getting this mobx warning, I don't know why I'm getting it since I'm using makeAutoObservable in my mobx store但是我收到了这个 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

My store我的店铺

export class UserStore
{
    constructor()
    {
        makeAutoObservable(this);
        
unreadChatIds=[];

setUnreadChatIds(payload)
    {
        this.unreadChatIds = payload;
    }
}

Why am I getting this error and how can I solve it?为什么我会收到此错误,我该如何解决? afaik if using makeAutoObservable and use my setter method as action I'm not changing mobx state directly. afaik 如果使用 makeAutoObservable 并使用我的 setter 方法作为操作,我不会直接更改 mobx state。

This warning occurs as you are modifying the userStore.unreadChatIds directly.当您直接修改 userStore.unreadChatIds 时会出现此警告。

To resolve this, you should modifying the value via action要解决此问题,您应该通过操作修改值

userStore.addChatId(e.message.chat_id);

Store店铺

export class UserStore
{
    constructor()
    {
        makeAutoObservable(this, {
        unreadChatIds: observable,
        setUnreadChatIds: action,
        addChatId: action,
    });
        
unreadChatIds=[];

setUnreadChatIds(payload)
    {
        this.unreadChatIds = payload;
    }

addChatId(id)
 {
   this.unreadChatIds.push(id);
 }
}
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM