Tracking only array length #3552
-
How come simply accessing the array length is tracked when the array stays the same size? import { action, autorun, observable } from "mobx"
let numbers = observable([1, 2, 3])
autorun(() => console.log(numbers.length))
action(() => (numbers[2] = 10))() In the above example, the tracked function inside Also, is there any way of tracking the array size only? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Atm observable array is backed by single
No, but you can wrap it in extra autorun(() => console.log(computed(() => numbers.length).get())) |
Beta Was this translation helpful? Give feedback.
-
Yes it is an intentional performance trade off, to avoid creating to many
subscriptions when looping / mapping over an array.
Note that a simpler way to achieve the above is reaction(() =>
array.length, length => { effect });
…On Thu, 27 Oct 2022, 14:59 Alejandro Hernandez, ***@***.***> wrote:
Thank you
—
Reply to this email directly, view it on GitHub
<#3552 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBCF3UFSUNAPSAKJPXTWFJ4EDANCNFSM6AAAAAARPOOEYM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Atm observable array is backed by single
Atom
. We would need a seperate atom forlength
. Tbh dunno if it's intentional, legacy reasons or oversight. cc @mweststrateNo, but you can wrap it in extra
computed
to prevent the outer derivation from re-running.