question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createViewModel with array

See original GitHub issue

I’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

https://codesandbox.io/s/minimal-mobx-react-project-6hthy

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
danielkczcommented, Jul 9, 2019

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)

1reaction
danielkczcommented, Jul 9, 2019

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? 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found