小编Gra*_*pes的帖子

是否可以乐观地更新端点的所有缓存?

在 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)

redux redux-toolkit rtk-query

4
推荐指数
1
解决办法
2196
查看次数

How to replace the contents of a group with a regular expression

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)

java regex

1
推荐指数
1
解决办法
49
查看次数

标签 统计

java ×1

redux ×1

redux-toolkit ×1

regex ×1

rtk-query ×1