Skip to content

Mobx reference type VS API data with entities relations #1926

Answered by EmilTholin
cavasinf asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @cavasinf!

You could model the users in your BookStore as well, and extract them from the books in your getBooks action and transform the response somewhat so that you only use the id of the author when you replace the books in the store.

Example (CodeSandbox)

const BookStore = types
  .model("BookStore", {
    users: types.optional(types.map(User), {}),
    books: types.optional(types.array(Book), [])
  })
  .actions((self) => ({
    getBooks: flow(function* () {
      const books = yield MagicalFetchBookApi();

      books.forEach((book) => {
        const { author } = book;

        self.users.set(author.id.toString(), author);
        self.books.push({
          ...book,
          a…

Replies: 3 comments 9 replies

Comment options

You must be logged in to vote
2 replies
@cavasinf
Comment options

@cavasinf
Comment options

Answer selected by EmilTholin
Comment options

You must be logged in to vote
5 replies
@cavasinf
Comment options

@EmilTholin
Comment options

@cavasinf
Comment options

@EmilTholin
Comment options

@cavasinf
Comment options

Comment options

You must be logged in to vote
2 replies
@EmilTholin
Comment options

@cavasinf
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants