Return updated query from store #448
Replies: 1 comment
-
there seem to be some confusing things in your example. For example, at query.then(({ first }) ... do you mean firstQuery.then(({ first }) ... ? Then in line setQuery(store.fetchAction); is the Anyways, although the docs state that you cannot use What you cannot do in an async actions is that you cannot set a property directly in the action, eg. .actions((self) => ({
doesNotWork: async () => {
const newSlug = await longRunningTask();
self.slug = newSlug;
},
}) You cannot set slug like this directly .actions((self) => ({
doesNotWork: async () => {
const newSlug = await longRunningTask();
self.slug = newSlug;
},
works: async () => {
const newSlug = await longRunningTask();
// Using an action is the key to making this work.
self.setSlug(newSlug);
},
// This action will work, even if it's called from an async function.
setSlug(s) {
self.slug = s;
},
}) I copied the examples from here and you can find more explanation here as well: https://coolsoftware.dev/blog/write-async-await-with-mobx-state-tree/ Would this kind of async action trick help with your issue? |
Beta Was this translation helpful? Give feedback.
-
Below is my scenario that I want achieve in an action in the root store:
Action in the store
In my HOC I use
useQuery
hook to get the dataIssue I face is, the query I am trying to return from the then block of secondQuery is never observed by the useQuery hook.
If we return the firstQuery instead of null, only the firstQuery gets observed in the HOC and I receive the initial data but could not get the newly formed data.
How can I make use of the useQuery hook to observe the queries returned from the then block within the action in the store.
I haven't found any solutions. since its a generic part I am working on, I won't be able to use async-await approach specifically. There are lot of actions in the store and the HOC uses setQuery on dynamic action names (unlike I mentioned above).
@jesse-savary or anyone please help. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions