#reactjs #redux #react-redux #redux-toolkit #rtk-query
Вопрос:
Я использую конечную точку с queryFn
вместо query
того, чтобы выполнять множество запросов. Есть ли способ вызвать конечные точки, которые уже определены, а не используются fetchWithBQ
?
вот пример.
export const api = createApi({
reducerPath: "api",
baseQuery: fetchBaseQuery({
baseUrl: "url",
}),
endpoints: (builder) => {
return {
device: builder.query<Device, string>({
query: (id) => `devices/${id}`, // repeat 1
}),
deployments: builder.query<Deployment[], string>({
queryFn: async (arg, _api, _extraOptions, fetchWithBQ) => {
// I would preferred to call the device endpoint directly.
// It will prevent to repeat the url and get cached data.
const result = await fetchWithBQ(`devices/${arg}`); // repeat 2
return ...
},
}),
};
},
});
Ответ №1:
Нет, на данный момент это невозможно, потому что это добавило бы отслеживание «что зависит от чего еще» ко всему, и это стало бы очень сложным для внутреннего управления.
Обычно вы выполняете зависимые запросы, просто используя два useQuery
крючка. И для абстракции, конечно, вы могли бы объединить их в крючок cusom.
const useMyCustomCombinedQuery = (arg) => {
const result1 = useMyFirstQuery(arg)
const result2 = useMySecondQuery(result1.isSuccess ? result1.data.something : skipToken)
return {result1, result2}
}