@hyper-fetch/react 9.8.1 → 9.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -170,39 +170,35 @@ const UserPosts = ({ userId }: { userId: number }) => {
|
|
|
170
170
|
```tsx
|
|
171
171
|
import { useSubmit } from "@hyper-fetch/react";
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
rollback: () => client.cache.set(cacheKey, previous),
|
|
190
|
-
// On success, refetch to get the real server data
|
|
191
|
-
invalidate: [cacheKey],
|
|
192
|
-
};
|
|
193
|
-
});
|
|
173
|
+
// setOptimistic runs before the request is sent - the comment list updates instantly
|
|
174
|
+
const addCommentOptimistic = addComment.setOptimistic(({ params, payload, optimistic }) => {
|
|
175
|
+
const temp = { id: `temp-${Date.now()}`, text: payload.text, author: "You" };
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
updates: [
|
|
179
|
+
optimistic(getComments.setParams({ postId: params.postId }))
|
|
180
|
+
.insert(temp)
|
|
181
|
+
// On success swap the temporary comment for the one returned by the server
|
|
182
|
+
.commit(({ data, response }) => data.map((comment) => (comment.id === temp.id ? response.data : comment)))
|
|
183
|
+
// If the request fails, take it out again
|
|
184
|
+
.rollback(({ data }) => data.filter((comment) => comment.id !== temp.id)),
|
|
185
|
+
],
|
|
186
|
+
context: { tempId: temp.id },
|
|
187
|
+
};
|
|
188
|
+
});
|
|
194
189
|
|
|
195
|
-
|
|
190
|
+
const AddComment = ({ postId }: { postId: number }) => {
|
|
191
|
+
const { submit, submitting, onSubmitError } = useSubmit(addCommentOptimistic);
|
|
196
192
|
|
|
197
|
-
// mutationContext is
|
|
193
|
+
// mutationContext is typed from the context returned by setOptimistic
|
|
198
194
|
onSubmitError(({ mutationContext }) => {
|
|
199
|
-
console.log("Rolled back
|
|
195
|
+
console.log("Rolled back comment", mutationContext?.tempId);
|
|
200
196
|
});
|
|
201
197
|
|
|
202
198
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
203
199
|
e.preventDefault();
|
|
204
200
|
const text = new FormData(e.currentTarget).get("text") as string;
|
|
205
|
-
submit({
|
|
201
|
+
submit({ params: { postId }, payload: { text } });
|
|
206
202
|
e.currentTarget.reset();
|
|
207
203
|
};
|
|
208
204
|
|
|
@@ -217,6 +213,8 @@ const AddComment = ({ postId }: { postId: number }) => {
|
|
|
217
213
|
};
|
|
218
214
|
```
|
|
219
215
|
|
|
216
|
+
The comment list is refreshed from the server once the request settles.
|
|
217
|
+
|
|
220
218
|
## License
|
|
221
219
|
|
|
222
|
-
[
|
|
220
|
+
[Apache-2.0](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/react",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.9.0",
|
|
4
4
|
"description": "React hooks and utils for the hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|