@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.
Files changed (2) hide show
  1. package/README.md +24 -26
  2. 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
- const AddComment = ({ postId }: { postId: number }) => {
174
- // setOptimistic runs BEFORE the request — update the list instantly
175
- const optimisticAdd = addComment.setParams({ postId }).setOptimistic((request, client) => {
176
- const cacheKey = getComments.setParams({ postId });
177
- const previous = client.cache.get(cacheKey);
178
-
179
- // Immediately insert the new comment into the cached list
180
- const optimisticComment = { id: `temp-${Date.now()}`, text: request.data.text, author: "You" };
181
- client.cache.set(cacheKey, {
182
- ...previous,
183
- data: [...(previous?.data || []), optimisticComment],
184
- });
185
-
186
- return {
187
- context: { previous },
188
- // If the request fails, put the old list back
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
- const { submit, submitting, onSubmitError } = useSubmit(optimisticAdd);
190
+ const AddComment = ({ postId }: { postId: number }) => {
191
+ const { submit, submitting, onSubmitError } = useSubmit(addCommentOptimistic);
196
192
 
197
- // mutationContext is fully typed from your setOptimistic return
193
+ // mutationContext is typed from the context returned by setOptimistic
198
194
  onSubmitError(({ mutationContext }) => {
199
- console.log("Rolled back — restored", mutationContext?.previous?.data?.length, "comments");
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({ data: { text } });
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
- [MIT](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
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.8.1",
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/",