【发布时间】:2021-12-31 22:55:08
【问题描述】:
像这个例子:constructing-a-dynamic-base-url-using-redux-state,我需要从缓存中获取某个端点的数据来构建fetch arg,我可以通过api.endpoints.getPost.select(postId)(getState())获取数据,但是如何确保getPost请求已经发送了吗?
const api = createApi({
baseQuery: fetchBaseQuery(),
endpoints: (builder) => ({
getPost: builder.query({
query: (id) => `/posts/${id}`,
}),
getBook: builder.query({
async queryFn(postId, { dispatch, getState }) {
// could I use this?
await dispatch(api.endpoints.getPost.initiate(postId))
const { data: post } = api.endpoints.getPost.select(postId)(getState())
// ...
// use post.xxx to fetch books...
myFetch(`/books/${post.xxx}`)
},
}),
}),
})
【问题讨论】: