Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed #3384
Replies: 2 comments
-
The warning says that you are updating observable outside an action. let copyy = userStore.unreadChatIds;
copyy.push(e.message.chat_id);
userStore.setUnreadChatIds(copyy); This code mutates |
Beta Was this translation helpful? Give feedback.
-
Right so after reading the documentation here are the options you have to get rid of this error. Option 1: import { makeAutoObservable } from "mobx"
class Store {
githubProjects = []
state = "pending" // "pending", "done" or "error"
constructor() {
makeAutoObservable(this)
}
fetchProjects() {
this.githubProjects = []
this.state = "pending"
fetchGithubProjectsSomehow().then(this.projectsFetchSuccess, this.projectsFetchFailure)
}
projectsFetchSuccess = projects => {
const filteredProjects = somePreprocessing(projects)
this.githubProjects = filteredProjects
this.state = "done"
}
projectsFetchFailure = error => {
this.state = "error"
}
} Option 2: import { action, makeAutoObservable } from "mobx"
class Store {
githubProjects = []
state = "pending" // "pending", "done" or "error"
constructor() {
makeAutoObservable(this)
}
fetchProjects() {
this.githubProjects = []
this.state = "pending"
fetchGithubProjectsSomehow().then(
action("fetchSuccess", projects => {
const filteredProjects = somePreprocessing(projects)
this.githubProjects = filteredProjects
this.state = "done"
}),
action("fetchError", error => {
this.state = "error"
})
)
}
} Option 3:
Option 4: import { runInAction, makeAutoObservable } from "mobx"
class Store {
githubProjects = []
state = "pending" // "pending", "done" or "error"
constructor() {
makeAutoObservable(this)
}
async fetchProjects() {
this.githubProjects = []
this.state = "pending"
try {
const projects = await fetchGithubProjectsSomehow()
const filteredProjects = somePreprocessing(projects)
runInAction(() => {
this.githubProjects = filteredProjects
this.state = "done"
})
} catch (e) {
runInAction(() => {
this.state = "error"
})
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm using mobx as state management for my react-native app, I'm modifying a simple array of ids like this:
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!
My store
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.
Beta Was this translation helpful? Give feedback.
All reactions