【发布时间】:2022-01-15 20:35:05
【问题描述】:
我是 Redux 和 RTK 查询的新手,我不明白如何在另一个端点的响应成功时从另一个端点获取数据。
我创建了一个这样的 API:
import { Config } from '@/Config'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
const baseQuery = fetchBaseQuery({ baseUrl: Config.API_URL })
const baseQueryWithInterceptor = async (args, api, extraOptions) => {
let result = await baseQuery(args, api, extraOptions)
if (result.error && result.error.status === 401) {
// Deal with unauthorised
}
return result
}
export const api = createApi({
baseQuery: baseQueryWithInterceptor,
endpoints: () => ({}),
})
我对每个资源都有一个模块,例如:
// /modules/matches
import { api } from '../../api'
import { fetchMatches } from '@/Services/modules/matches/fetchMatches'
export const matchApi = api.injectEndpoints({
endpoints: build => ({
fetchMatches: fetchMatches(build),
}),
overrideExisting: false,
})
export const { useFetchMatchesQuery } = matchApi
// /modules/matches/fetchMatches
export const fetchMatches = build => {
return build.query({
query: type => ({ url: `matches/${type}` })
})
}
因此,在我的组件中,我将其称为:
const { data: matches, error, isLoading } = useFetchMatchesQuery('explorer')
现在,当useFetchMatchesQuery 成功时我需要做的是:
- 使用来自
useFetchMatchesQuery响应数据的所有匹配 id 创建一个数组 - 调用另一个查询以获取参数中带有
matchsIds的其他数据 - 在渲染
matches数据的同一组件中使用响应。
谢谢你的帮助????
【问题讨论】:
标签: javascript react-native redux rtk-query