我正在使用“Redux Toolkit Query”并遇到我有一组地点 id 并想要返回一组检索到的地点的情况。但是,使用我当前的设置,我只能查询一个地方,而不是整个编译的地方列表。
我调用查询的钩子:
function ShowPlaceList() {
const placeIDs = [1, 2, 3, 4, 5];
const { data, isFetching, isLoading } = useGetPlaceByIdQuery({
placeID: placeIDs,
});
// data should be an array
return (
<div>
{data.map(function (item, i) {
return <ShowPlace {...item} />;
})}
</div>
);
}
简化切片:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
export const placeApi = createApi({
reducerPath: 'placeApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://providePlace.co/api/v2/' }),
endpoints: (builder) => ({
getGetPlaceById: builder.query({
query: ({placeID}) => `place/${placeID}`,
}),
}),
})
export const { useGetPlaceByIdQuery } = placeApi
我很高兴任何提示!