我正在尝试在不使用自动生成的查询钩的情况下从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?
发布评论
评论(2)
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 intouseSelector(selector)
.If you write that altogether, you end up with
api.endpoints.getPosts.select()(state)
.@phry
谢谢您的回答!我不是100%确定我理解您的答案。但这使我朝着使我获取数据的方向。
我最终创建了像文档这样的选择器。
在我的功能中,我从configurestore()方法中获取导出的商店进行了引用,
但我不确定这是否是预期的方法。
@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.
and in my function, I referenced it with getting the exported store from configureStore() method
But I'm not sure if this is the intended way to do it.