-
I have a model and an entity: const productModel = types.model({
id: types.identifier,
name: types.string
})
type ProductIn = SnapshotIn<typeof productModel>
const product = productModel.create({
id: '1',
name: 'product1'
}); Now how can I update the entity in a manner of a form submit hook: const onSubmit = (data: ProductIn) => {
const {id, ...rest} = data;
runInAction(() => {
// If there was merge():
this.selectedProduct?.merge(rest)
// Or if some mobxMerge() existed:
mobxMerge(this.selectedProduct, rest)
})
} P.S. Btw, am I correct to use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
It seems like a trivial export const productModel = types
...
.actions((self) => ({
...
update(data: ProductIn) {
const { id: _, ...rest } = data
Object.assign(self, rest)
},
|
Beta Was this translation helpful? Give feedback.
-
If you just have a bunch of properties you can use applySnapshot(this.selectedProduct, { ...getSnapshot(this.selectedProduct), ...rest }); |
Beta Was this translation helpful? Give feedback.
If you just have a bunch of properties you can use
applySnapshot
. I'm pretty sure it requires a fully valid snapshot though, so you may need to do something like this if you only have a partial set of properties coming from your form: