August 2017
Beginner
374 pages
10h 41m
English
We have already discussed this previously--you may prefer to store your data in objects with their database ID as the key, and the data entry as the value. Such an object is called an index, and structuring the state as an index makes it easy to select and update the data.
For example, we could store our posts as follows:
{ posts: { "post1": { id: "post1", author: "dan", text: "hello world", comments: ["comment1"] }, "post2": { ... }, ... }}
We can access a single post via state.posts[postId]. Using an array, we would have to use .find() to loop through all posts and find the one with the correct id.
Read now
Unlock full access