Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samskiter opened this issue Jun 17, 2024 · 8 comments
Open

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

samskiter opened this issue Jun 17, 2024 · 8 comments
Labels
✌ duplicate This issue or pull request already exists

Comments

@samskiter
Copy link

samskiter commented Jun 17, 2024

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
Copy link
Author

samskiter commented Jun 17, 2024

This works, but the above indexing doesn't:

arrayActions.set(todoStore.todos, index, new Todo({
    done: true,
}))

@xaviergonz
Copy link
Owner

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

@samskiter
Copy link
Author

samskiter commented Jun 17, 2024

Aha - I wonder if it's related to this: #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 xaviergonz added the ✌ duplicate This issue or pull request already exists label Jun 17, 2024
@xaviergonz
Copy link
Owner

Sounds like you found it :)

@samskiter
Copy link
Author

samskiter commented Jun 17, 2024 via email

@xaviergonz
Copy link
Owner

xaviergonz commented Jun 17, 2024

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
Copy link
Author

samskiter commented Jun 17, 2024 via email

@xaviergonz
Copy link
Owner

https://babeljs.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✌ duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants