Reputation: 83
Create API
export const pollingRatesApi = createApi({
reducerPath: 'pollingRates',
baseQuery: fetchBaseQuery({ baseUrl: configuredBaseURL }),
endpoints: (builder) => ({
getRates: builder.query<RatesStreamLongPollingData, RatesStreamParams>({
queryFn: async (arg) => await api.get(getRatesStreamUrl(arg)),
}),
}),
});
export const { useLazyGetRatesQuery, useGetRatesQuery } = pollingRatesApi;
Component A loads data
const [getRates, result] = useLazyGetRatesQuery();
const handleGetRates = async (params) => {
await getRates({ ...params, pollingInterval: 1000 });
};
Child component B trying to get already uploading data
const [, { currentData, isLoading, data }] = useLazyGetRatesQuery(undefined, {
selectFromResult: ({ data, error, isLoading }) => ({
data,
error,
isLoading,
}),
pollingInterval: 1000,
});
in child compoenent data === undefined all the time. why so? I use useLazy****Query because I can only form a link for a query after a series of other queries.
An attempt to get data through a selector in Child Components also returns undefined
const rRates = (state) => pollingRatesApi.endpoints.getRates.select()(state).data;
console.log(useSelector(rRates));
Upvotes: 0
Views: 851
Reputation: 44276
getRates(undefined)
and getRates(params)
are two distinct cache entries. They would only give you a shared result if params
were undefined
, otherwise they make two independent requests to the server.
You would probably have to share those params
between the two components, either by storing them in the Redux state or passing them as props.
Also, please note that getRates({ ...params, pollingInterval: 1000 });
will not set a pollingInterval.
You have to pass options into useLazyGetRatesQuery
, not into the trigger function:
const [getRates, result] = useLazyGetRatesQuery({ pollingInterval: 1000 });
Upvotes: 1