createViewModel with array
See original GitHub issueI’m trying to use createViewModel
to make a viewModel of an object with an array. When I edit the array property of the viewModel, the changes are immediately reflected on the original object. Not sure if this is a bug or if I’m using this wrong. Help would be much appreciated!
import { observable } from "mobx";
import { createViewModel } from "mobx-utils";
class AppState {
@observable user = {
arr: [1]
};
}
const appState = new AppState();
const userState = createViewModel(appState.user);
console.log(appState.user.arr[0]); // 1
console.log(userState.arr[0]); // 1
userState.arr[0] = 5;
// User.arr[0] should still be 1
console.log(appState.user.arr[0]); // 5
console.log(userState.arr[0]); // 5
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
createViewModel · MobX Guide - mamaya-spark
You may use observable arrays, maps, and objects with createViewModel , but keep in mind to assign fresh instances of those to the...
Read more >How to create view model for API with response of array in ...
Write one function in your View Model to get the data from service class. class sampleViewModelClass { typealias successHandler = (_ ...
Read more >4-Nodejs MVC Knockout Project2-Create view model with array
4-Nodejs MVC Knockout Project - Knockout - Create view model with array of objects- observableArray, applyBindings.
Read more >Component registration - Knockout.js
A constructor function; A shared object instance; A createViewModel factory ... templateNodes array is useful if you want to build a component that...
Read more >Recommended Ways To Create ViewModel or ...
Kotlin examples to show different ViewModel and AndroidViewModel implementations There are few ways... Tagged with android, kotlin, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, I am not familiar with this particular utility. By reading docs, it indeed should behave the way you are saying. Probably best if someone else who knows about it can have a look.
ping @ItamarShDev (you did some changes to it a while ago)
And when would expect the value change would happen? MobX is not asynchronous, it does the change right away. What would be even use case where you need to see old values after changing it? 😃