Replies: 4 comments 5 replies
-
Can you elaborate with a fuller example? Hard to say what are you are trying to achieve. Ideally a runnable minimal reproduction. |
Beta Was this translation helpful? Give feedback.
-
in this example second useEffect fired once on start( but items is not yet loaded), but not fired when items is loaded( categoryItems changed ) |
Beta Was this translation helpful? Give feedback.
-
categoryItems is not being used by your render, so `observer` doesn't
track it, causing the effect not to be re-evaluated. It can be fixed
by slicing categoryItems before using it as dep, which would be
correct from react perspective, but I think it is a weird mental
model, as it directly couples your effects with rendering, which is
why we recommend using autorun instead which decouples the execution
of your effect from the fact whether your component re-rendered or
not.
…On Mon, Nov 30, 2020 at 10:01 AM Aleksandr Popov ***@***.***> wrote:
class CategoryStore {
categoryItems = [];
constructor (){
makeAutoObservable(this, {
getProductsByCategoryId: action
})
}
async getProductsByCategoryId(categoryId){
.....
runInAction(() => {
this.categoryItems.replace(result)
})
}
}
let component = observer({categoryId, filter}=>{
const {categoryStore } = useStores()
const [categories, setCategories] = useState([])
const { categoryItems } = categoryStore
useEffect( ()=>{
if ( categoryId ) {
(async () => {
await categoryStore.getProductsByCategoryId(categoryId)
})()
}
}, [categoryId] )
useEffect( ()=>{
if (categoryItems && childCategories ) {
let result = //....some calculations
setCategories(result)
}
}, [categoryId, categoryItems] )
})
in this example second useEffect fired once on start( but items is not yet
loaded), but not fired when items is loaded( categoryItems changed )
It works if I put autorun inside it, but its not clear why I should do it.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2652 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBANGSO6CULGL4CB3BDSSNUQNANCNFSM4UG2W7NQ>
.
|
Beta Was this translation helpful? Give feedback.
-
If you create a new effect by changing deps, the old autorun is disposed
(that is why returning the autorun is important) and a new one is set up.
So yes, that works as expected.
…On Mon, Nov 30, 2020 at 12:17 PM Daniel K. ***@***.***> wrote:
Adding deps makes sense only in case the effect depends on non-observable
value. I suppose in your case it's componentParam? And I guess useParams
is not working with observables too. However, in that case, consider if you
really need to rerun the side effect based on those changes. Sometimes it
might make more sense to split into more effects. There is no golden bullet
here.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2652 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBH4YSP7EFKMPYVOTZ3SSOEONANCNFSM4UG2W7NQ>
.
|
Beta Was this translation helpful? Give feedback.
-
Im widely use
but its not working in mobx6, seems now I should rewrite it to use with autorun.
is it any way to work in old way ?
Beta Was this translation helpful? Give feedback.
All reactions