WaspW
Wasp16mo ago
JakeLoew

How to update data coming from `useQuery`

Let's say you have the following code in an app that has a model Author as well as model Book where each book belongs to one author...
import { getAuthor, createBook } from 'wasp/client/operations'

function MyComponent({ authorId }: { authorId: string; }) {
  const { data: author } = useQuery(getAuthor, { id: authorId }) // populates author with books
  const createNewBookByAuthor = async (authorId: number) => {
    await createBook({ title: "new book!", authorId });
  }

  return <div>
    <button onClick={createNewBookByAuthor}>Create New Book</button>
    <ul>
      {author.books.map(book => {
        <li>
          {book.title}
        </li>
       })}
    </ul>
  </div>
}


What is a good pattern to update data coming from the getAuthor query when a new book is created?
Was this page helpful?