xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
544 stars 23 forks source link

How do I mutate an object inside of an array? #545

Open samskiter opened 2 months ago

samskiter commented 2 months ago

Is mutating an item inside of an array, like this todoStore.todos[index].setDone(true) supported?

export class TodoModel extends Model({
  todos: prop<Todo[]>(() => []).withSetter(),
})

I'm finding that this isn't consistently working, especially after first adding a new Todo to the model

samskiter commented 2 months ago

This works, but the above indexing doesn't:

arrayActions.set(todoStore.todos, index, new Todo({
    done: true,
}))
xaviergonz commented 2 months ago

todoStore.todos[index].setDone(true) should work

samskiter commented 2 months ago

Aha - I wonder if it's related to this: https://github.com/xaviergonz/mobx-keystone/issues/173

My todo was created like

export class Todo extends Model({
  done: prop<bool>().withSetter(),
}) {
  static defaultTodo() {
    return new Todo({ done: false })
  }
}

export class TodoModel extends Model({
  todos: prop<Todo[]>(() => []).withSetter(),
}) {
  @modelAction
  addNewTodo() {
  this.setTodos([...this.todos, Todo.defaultTodo()])
}

But If i change to this, things start working:

export class Todo extends Model({
  done: prop<bool>().withSetter(),
})

export class TodoModel extends Model({
  todos: prop<Todo[]>(() => []).withSetter(),
}) {
  @modelAction
  addNewTodo() {
  this.setTodos([...this.todos, new Todo({ done: false })])
}
xaviergonz commented 2 months ago

Sounds like you found it :)

samskiter commented 2 months ago

Yes, but unsure why a static factory method wouldn't work!

On Mon, 17 Jun 2024, 18:34 Javier Gonzalez, @.***> wrote:

Sounds like you found it :)

— Reply to this email directly, view it on GitHub https://github.com/xaviergonz/mobx-keystone/issues/545#issuecomment-2173961494, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJWOW3EGF6KAM4SOISRURLZH4MYRAVCNFSM6AAAAABJOMQEFSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZTHE3DCNBZGQ . You are receiving this because you authored the thread.Message ID: @.***>

xaviergonz commented 2 months ago

It works if you use anything other than babel to transpile the legacy decorators (e.g. typescript compiler) and potentially it might work even with babel if you use the new decorators.

samskiter commented 2 months ago

Hrmm, I'm not consciously using Babel - I've never even heard of it. Can you point me at something to read up?

On Mon, 17 Jun 2024, 19:14 Javier Gonzalez, @.***> wrote:

It works if you use anything other than babel to transpile the legacy decorators (e.g. typescript compiler) and potentially it might work even with babel if you use the new decorators.

— Reply to this email directly, view it on GitHub https://github.com/xaviergonz/mobx-keystone/issues/545#issuecomment-2174031977, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJWOW3FSNG6CZUUNGHSVDDZH4RRJAVCNFSM6AAAAABJOMQEFSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZUGAZTCOJXG4 . You are receiving this because you authored the thread.Message ID: @.***>