When reading the redux document, I found that the document mentioned this:
However, you should do everything possible to maintain a serializable state. Do not invest anything in it that you cannot easily turn into JSON.
So my question is, what is the use of maintaining state serialization? Or, what difficulties can I have if I put non-serializable data in the storage?
And I believe that this is not unique to redux - Flux, even the local state of React Local offers the same thing.
So I understand, here is an example. Suppose the store structure is similar to this.
{ books: { 1: { id: 1, name: "Book 1", author_id: 4 } }, authors: { 4: { id: 4, name: "Author 4" } } }
Everything should look good. However, when I try to access the "author of book 1", I have to write this code:
let book = store.getState().books[book_id]; let author = store.getState().authors[book.author_id];
Now I'm going to define a class:
class Book { getAuthor() { return store.getState().authors[this.author_id]; } }
And my store will be:
{ books: { 1: Book(id=1, name="Book 1") }, ... }
So that I can easily get the author using:
let author = store.getState().books[book_id].getAuthor();
The second approach can make the book object aware of how to get the author’s data, so the caller does not need to know the relationship between the books and the authors. Then why don’t we use it, instead of storing a “simple object” in a store just like approach No. 1?
Any ideas are welcome.
javascript reactjs redux flux
charlee
source share