From the course: TanStack Query: Efficient Data Fetching and State Management
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Working with mutations - TanStack Query Tutorial
From the course: TanStack Query: Efficient Data Fetching and State Management
Working with mutations
- [Instructor] So far, we've learned how to fetch data with useQuery and useQueries. But what about changing data on the server? Well, that's where mutations come in. Let's take a look at the useMutation hook. These mutations are going to help us modify data, they're going to run only when we explicitly trigger them, and then they can help us do stuff like track loading state and handle errors. So back to our code here, we're going to update this code and introduce a mutation pattern that you'll see in a TanStack Query project. So the first step that we need to take here is we want to create a function. This will live outside of our GitHubProfiles function, and it's going to be called saveFavorite. So we could think about this function as going to the data and actually making a change. For now, we're just going to set a timeout to model what that behavior would look like. So let's go ahead and model the data First, we're going to say string, and then we will set a value called…