Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  135] [ 1]  / answers: 1 / hits: 24660  / 8 Years ago, thu, march 10, 2016, 12:00:00

I am trying to understand how to use observable array with Mobx.



I have a hard time to figure out why this:



let entities = observable([]);
entities[0] = foo;
autorun(() =>{
console.log(entities);
});


writes:



[$mobx: Object]
0: (...)
1: (...)
2: (...)
3: (...)
4: (...)
5: (...)
6: (...)
7: (...)
8: (...)
9: (...)
10: (...)
11: (...)
12: (...)
13: (...)
14: (...)
15: (...)
16: (...)
17: (...)
...
999: (...)


Instead of a classic array?


More From » arrays

 Answers
1

Figure out!



As stated in the docs




Bear in mind that Array.isArray(observable([])) will yield false, so whenever you need to pass an observable array to an external library, it is a good idea to create a shallow copy before passing it to other libraries or built-in functions (which is good practice anyway) by using array.slice() or array.peek(). So Array.isArray(observable([]).slice()) will yield true.




The doc exemple show us a todos.filter() which could lead to confusion because todos looks like a real JS Array. But it is not.



So for my exemple to work I just have to console.log(entities.slice()) which will display a real JS array.


[#62983] Tuesday, March 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rebekahalysah

Total Points: 304
Total Questions: 96
Total Answers: 102

Location: Taiwan
Member since Mon, May 2, 2022
2 Years ago
;