How use reaction with flow? #1919
-
Hi! In my case, I need to make a request to the server when changing the field. The data acquisition function is implemented using flow. When I try to call a function in reaction, I get an error:
If you redo the method of receiving data on async \await, then everything works as it should. But I would like to use flow as recommended by the library. I assume that in the case of flow, there is a loss of this context. Unfortunately, I could not solve this problem on my own. const fetchData = flow(function* () {
// ...
let result = yield getData();
// ...
});
function afterCreate() {
const disposerReaction = reaction(
() => self.field,
flow(function* () {
yield fetchData ();
})
);
addDisposer(self, disposerReaction)
} in the case when the store is not a descendant of another, then this problem can be solved by a hack in the form of: function afterCreate() {
const disposerReaction = reaction(
() => self.field,
flow(function* () {
yield getRoot(self).fetchData();
})
);
addDisposer(self, disposerReaction)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Yrsafam! You could probably put // ...
.actions((self) => ({
fetchData: flow(function* () {
// ...
let result = yield getData();
// ...
}),
}))
.actions((self) => ({
afterCreate() {
const disposerReaction = reaction(
() => self.field,
self.fetchData
);
addDisposer(self, disposerReaction);
},
})); |
Beta Was this translation helpful? Give feedback.
Hi @Yrsafam!
You could probably put
fetchData
in a separateactions
block to have it always have a parent context: