You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-4Lines changed: 13 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -65,8 +65,8 @@ Polling is paused when the window lose focus. To fix that issue we can set `refe
65
65
66
66
Combine polling with callbacks. Use the `refetchInterval` option to pull the api data every 3 seconds. Behind the scenes add a fourth superhero of your choice to the superheroes array in `db.json`.
67
67
68
-
a.) Within the onSuccess callback check if the number of heroes is 4 and ifit is the case I want you to stop the polling.
69
-
b.) Within the onError callback I want you to stop the polling.
68
+
a) Within the onSuccess callback check if the number of heroes is 4 and ifit is the case I want you to stop the polling.
69
+
b) Within the onError callback I want you to stop the polling.
70
70
71
71
Hint:
72
72
Mantain state variable whose initial value is 3000. State variable will be assigned to `refetchInterval` configuration. In callbacks check for the response / errors and set the state variable to false.
@@ -83,9 +83,9 @@ In our example we transformed data to be an array of just heroes names instead o
83
83
84
84
We can query by id in two ways (look commits under branch name: `feature/12-queryById`).
85
85
86
-
a.) Solution 1 or first commit: **QueryById with manuali passed ID**
86
+
a) Solution 1 or first commit: **QueryById with manuali passed ID**
87
87
- We can manually pass id into fetch function: file `useSuperHeroData`
88
-
b.) Solution 2 or second commit: **QueryById with react-query automatic passed id**
88
+
b) Solution 2 or second commit: **QueryById with react-query automatic passed id**
89
89
- React query automatically pass id into fetch function: file `useSuperHeroData`
90
90
91
91
**9. react-query - Parallel queries**
@@ -108,6 +108,15 @@ If we add flag `keepPreviousData` into the useQuery options, when we paginate to
108
108
109
109
Handling mutation response will save you additional network request.
110
110
111
+
**14. react-query - Optimistic updates**
112
+
Updating the state before performing a mutation under the assumption that nothing can go wrong.
113
+
114
+
`onMutate:` is called before the mutation function is fired and is passed the same variables that mutation function would receive.
115
+
a) Cancel any outgoing refetches so they don't override our optimistic update.
116
+
b) Get a hold of the current query data before we make any update. This will help us role back in case the mutation fails.
117
+
`onError:` is called if the mutation encounters an error.
118
+
`onSettled:` is called if the mutation is either successfull or encounters an error. `invalidateQueries` will ensure client state is in sync with server state. The user will not notice a difference.
0 commit comments