RTKQ-选择无钩的数据

发布于 2025-02-13 07:03:58 字数 633 浏览 1 评论 0 原文

我正在尝试在不使用自动生成的查询钩的情况下从RTKQ中选择缓存数据,但是我很难理解 docs

const result = api.endpoints.getPosts.select()(state)
const { data, status, error } = result

这就是文档描述如何访问数据的方式,但是我找不到有关如何注入状态对象“ select()( state )”的任何引用。

如果我只调用SELECT,我不知道如何访问数据?

api.endpoints.getPosts.select()

有人可以向我解释“ select()”和“ select()(state)”之间的区别

,或者从RTKQ访问缓存数据的最佳解决方案是什么?

I'm trying to select cached data from RTKQ without using the auto-generated query hook, but I'm having trouble understanding the docs

const result = api.endpoints.getPosts.select()(state)
const { data, status, error } = result

This is how the docs describe how to access the data, but I can't find any references on how to inject the state object "select()(state)".

I can't figure out how to access the data if I only call the select?

api.endpoints.getPosts.select()

Can someone explain me the difference between "select()" and "select()(state)"

Or what is the optimal solution to access the cached data from RTKQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2025-02-20 07:03:58

api.endpoints.getPosts.Select()的结果是选择器函数,因为使用“ GetPosts”端点而没有参数。

同样, api.endpoints.getPosts.Select的结果({page:5})选择器函数使用“ getPosts”端点,参数 {page:5}

然后将选择器函数称为 selector(state)或传递到 useselector(selector)

如果您完全编写那本书,则最终会以 api.endpoints.getposts.select()(state)>。

The result of api.endpoints.getPosts.select() is a selector function for the result of using the "getPosts" endpoint without arguments.

Similarly, result of api.endpoints.getPosts.select({ page: 5 }) is a selector function for the result of using the "getPosts" endpoint the argument { page: 5 }.

A selector function is then called as selector(state) or passed into useSelector(selector).

If you write that altogether, you end up with api.endpoints.getPosts.select()(state).

左岸枫 2025-02-20 07:03:58

@phry

谢谢您的回答!我不是100%确定我理解您的答案。但这使我朝着使我获取数据的方向。

我最终创建了像文档这样的选择器。

export const selectUser = (state) => userApi.endpoints.getUser.select()(state);

在我的功能中,我从configurestore()方法中获取导出的商店进行了引用,

const { data } = selectUser(store.getState());

但我不确定这是否是预期的方法。

@phry

Thank you for your answer! I'm not 100% sure I understood your answer. But it pointed me in a direction that enabled me to get the data.

I ended up creating a selector like the docs.

export const selectUser = (state) => userApi.endpoints.getUser.select()(state);

and in my function, I referenced it with getting the exported store from configureStore() method

const { data } = selectUser(store.getState());

But I'm not sure if this is the intended way to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文