Question: observable array push does not react. #2738
Answered
by
mweststrate
HwangTaehyun
asked this question in
Q&A
Replies: 2 comments 4 replies
-
Please, can you provide runnable reproduction? It's kinda to understand what's wrong. |
Beta Was this translation helpful? Give feedback.
1 reply
-
… On Fri, Jan 22, 2021 at 2:14 AM Taehyun Hwang ***@***.***> wrote:
Hi, I use mobx everyday!.
Recently, I don't understand following autorun's working logic. Please
help me..
import { autorun, observable, makeAutoObservable } from 'mobx'
class Message {
title
author
likes
constructor(title, author, likes) {
makeAutoObservable(this, {likes: observable.shallow})
this.title = title
this.author = author
this.likes = likes
}
updateTitle(title) {
this.title = title
}
pushLikes(item) {
this.likes.push(item);
}}
let message = new Message("Foo", { name: "Michel" }, ["Joe", "Sara"])
autorun(() => {
console.log(message.title)})
autorun(() => {
(1) // is called
console.log(message.likes.slice())
(2) // is called
message.likes.map((item)=>{
console.log(item);
})
(3) // is not called
console.log(message.likes);})
message.pushLikes("Taehyun");
I expect that when I call Message class's "pushLikes" method then (3)
should be called because Message class's likes array is observed by shallow
strategy.
I understand shallow observe means that tracking whether observable
array's each item's reference has changed or new item is added.
Why (3) does not react?
Thank you for reading my question!!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2738>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBCFDKCP5E6ZRLSBNQLS3DNP7ANCNFSM4WNZXIVQ>
.
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
danielkcz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I use mobx everyday!
Recently, I don't understand following autorun's working logic. Please help me..
I expect that when I call Message class's "pushLikes" method then (3) should be called because Message class's likes array is observed by shallow strategy.
I understand shallow observe means that tracking whether observable array's each item's reference has changed or new item is added.
Why does not case (3) react?
Maybe this has sth to do with below. (https://mobx.js.org/understanding-reactivity.html)
Changing the question, how should I use the observe step of an observable array in what situations?
Thank you for reading my question!!
Beta Was this translation helpful? Give feedback.
All reactions