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: src/content/6/zh/part6d.md
+43-13Lines changed: 43 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,7 +142,7 @@ The return value of the <i>useQuery</i> function is an object that indicates the
142
142
<div>loading data...</div>
143
143
```
144
144
145
-
However, the HTTP request is completed so quickly that even the most astute will not be able to see the text. When the request is completed, the component is rendered again. The query is in the state <i>success</i> on the second rendering, and the field <i>data</i> of the query object contains the data returned by the request, i.e. the list of notes that is rendered on the screen.
145
+
<!--However, the HTTP request is completed so quickly that even the most astute will not be able to see the text. When the request is completed, the component is rendered again. The query is in the state <i>success</i> on the second rendering, and the field <i>data</i> of the query object contains the data returned by the request, i.e. the list of notes that is rendered on the screen.-->
@@ -186,9 +186,13 @@ The current code for the application is in [GitHub](https://github.com/fullstack
186
186
187
187
### 使用 React Query 将数据同步至服务器
188
188
189
-
Data is already successfully retrieved from the server. Next, we will make sure that the added and modified data is stored on the server. Let's start by adding new notes.
189
+
<!--Data is already successfully retrieved from the server. Next, we will make sure that the added and modified data is stored on the server. Let's start by adding new notes.-->
190
190
191
-
Let's make a function <i>createNote</i> to the file <i>requests.js</i> for saving new notes:
To create a new note, a [mutation](https://react-query-v3.tanstack.com/guides/mutations) is defined using the function [useMutation](https://react-query-v3.tanstack.com/reference/useMutation):
232
+
<!--To create a new note, a [mutation](https://react-query-v3.tanstack.com/guides/mutations) is defined using the function [useMutation](https://react-query-v3.tanstack.com/reference/useMutation):-->
The parameter is the function we added to the file <i>requests.js</i>, which uses Axios to send a new note to the server.
240
+
<!--The parameter is the function we added to the file <i>requests.js</i>, which uses Axios to send a new note to the server.-->
233
241
234
-
The event handler <i>addNote</i> performs the mutation by calling the mutation object's function <i>mutate</i> and passing the new note as a parameter:
<!--The event handler <i>addNote</i> performs the mutation by calling the mutation object's function <i>mutate</i> and passing the new note as a parameter:-->
Our solution is good. Except it doesn't work. The new note is saved on the server, but it is not updated on the screen.
253
+
<!--Our solution is good. Except it doesn't work. The new note is saved on the server, but it is not updated on the screen.-->
254
+
255
+
我们的解决方案挺不错,但仍美中不足。新的笔记虽然存储在了服务器上,但并没有在屏幕上更新。
256
+
257
+
<!--In order to render a new note as well, we need to tell React Query that the old result of the query whose key is the string <i>notes</i> should be [invalidated](https://react-query-v3.tanstack.com/guides/invalidations-from-mutations).-->
242
258
243
-
In order to render a new note as well, we need to tell React Query that the old result of the query whose key is the string <i>notes</i> should be [invalidated](https://react-query-v3.tanstack.com/guides/invalidations-from-mutations).
Now that the mutation has been successfully executed, a function call is made to
282
+
<!--Now that the mutation has been successfully executed, a function call is made to-->
283
+
284
+
在突变已经成功执行后,一个函数被调用
265
285
266
286
```js
267
287
queryClient.invalidateQueries('notes')
268
288
```
269
289
270
-
This in turn causes React Query to automatically update a query with the key <i>notes</i>, i.e. fetch the notes from the server. As a result, the application renders the up-to-date state on the server, i.e. the added note is also rendered.
290
+
<!--This in turn causes React Query to automatically update a query with the key <i>notes</i>, i.e. fetch the notes from the server. As a result, the application renders the up-to-date state on the server, i.e. the added note is also rendered.-->
So again, a mutation was created that invalidated the query <i>notes</i> so that the updated note is rendered correctly. Using mutation is easy, the method <i>mutate</i> receives a note as a parameter, the importance of which has been changed to the negation of the old value.
The application works well, and the code is relatively simple. The ease of making changes to the list of notes is particularly surprising. For example, when we change the importance of a note, invalidating the query <i>notes</i> is enough for the application data to be updated:
0 commit comments