I want to create a deep copy of an object that has mobx observable and computed properties.
The goal is to create copy item for local editing, that can be committed or discarded.
I've found an example of this behavior but it is from mobx-state-tree:

How can I achieve the same behaviour in mobx?
You can use the createViewModel from the mobx-utils package.
Example
class Todo {
@observable firstName = "Foo";
@observable lastName = "Bar";
@computed get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
const todo = new Todo();
const todoCopy = createViewModel(todo);
todoCopy.firstName = "Baz";
// ...
// Submit the changes to the original todo
todoCopy.submit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With