在 redux 教程中,我们学习了如何执行乐观更新:
第 8 部分-rtk-query-advanced#implementing-optimistic-updates
但在updateQueryData的 API 参考中,我发现args必须传入 an :
const updateQueryData = (
endpointName: string,
args: any,
updateRecipe: (draft: Draft<CachedState>) => void
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>;
Run Code Online (Sandbox Code Playgroud)
所以,如果我们的帖子有分页,
getPosts: builder.query({
query: (current: number) => `/posts?current=${current}`,
providesTags: ['Post']
}),
Run Code Online (Sandbox Code Playgroud)
这个时候我们如何进行乐观更新呢?
// Is there a way to update all getPosts caches?
// Am I missing some documents?
apiSlice.util.updateQueryData('getPosts', ???, (draft) => {
const post = draft.find((post) => post.id === postId)
if (post) {
post.reactions[reaction]++
}
})
Run Code Online (Sandbox Code Playgroud) This is the original string:
233<C:\Users\Grapes\Documents\title.png>233<C:\Users\Grapes\Documents\title.png>33
This is the replaced string I want:
233<1>233<2>33
I want to replace the file path in the string with the id I got after uploading to the server, but my program is in an infinite loop.
public void sendMessage(String msg) {
new Thread(()-> {
var pat = Pattern.compile("<(.*?[^\\\\])>");
var matcher = pat.matcher(msg);
int k = 0;
while (matcher.find()) {
matcher.replaceFirst("<" + k++ + ">"));
}
System.out.println(msg);
}).start();
}
Run Code Online (Sandbox Code Playgroud)