Confused on which type(s) to use when how to store a large object with dynamic keys #1893
-
Coming from redux here and I'm not sure how to store normalized data (or do I even need to?). Say I have a todo list I'd normalize my data so instead of an array of todo objects, I'd change it to the below and store it in redux as such:
With MST, it seems like I'd need 2 models, TodoList and Todo. However I'm not sure how I would have this data normalized. Seems like docs suggest keeping the array so I'd have something like this:
But ideally I have it the way I would normalized in redux |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @andrewdang17! Normalization is often nicely handled with the help of import { types } from "mobx-state-tree";
const Todo = types.model({
id: types.identifierNumber,
name: types.string
});
const RootStore = types.model({
todoIds: types.array(types.reference(Todo)),
todos: types.map(Todo),
selectedTodo: types.reference(Todo)
});
const rootStore = RootStore.create({
todoIds: [1, 2, 3],
todos: {
1: { id: 1, name: "Foo" },
2: { id: 2, name: "Bar" },
3: { id: 3, name: "Baz" }
},
selectedTodo: 1
});
// References are evaluated to their corresponding model instance
console.log(rootStore.selectedTodo)
// => {id: 1, name: "Foo"}
console.log(rootStore.todoIds[2])
// => {id: 3, name: "Baz"} |
Beta Was this translation helpful? Give feedback.
Hi @andrewdang17!
Normalization is often nicely handled with the help of
references
.