EntityAdapter selector issues #3815
-
Let me start by saying I'm learning how to use all this and playing with different ways of getting the data. I am trying to call a selector from my entityAdapter by writing
And in the console I see the error "error: No data found at Here's my itemApiSlice
and here's my store
I'm willing to bet I'm overlooking something everyone sees as obvious, I just can't figure out what it is. I don't see to have an item getting the data like this
So I know the data is there somewhere, I just can't figure out why the selector isn't working. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I think the immediate issue is this line: export const selectItemsResult = itemsApiSlice.endpoints.getItems.select()
In other words, if I want to make a selector that returns the cache entry for Pikachu in a Pokemon API, I'd need to do If you don't pass in a query arg, that's the same as In your case, you are creating a selector with a query arg of So, the selector is trying to look up a cache entry that doesn't exist, that returns Generally, you shouldn't call That said, there's a couple complications. You have normalized the data stored in that cache entry, but you want to read the values out as an array. Again, totally doable, but you need to be careful how it's done. The So, the best answer here is probably something like: function MyComponent() {
// maybe a prop, or state, or whatever
const getItemsQueryArg = 'itemsList';
const entitySelectors = useMemo(() => {
// make a unique copy of the selectors for this component
return entityAdapter.getSelectors();
}, [getItemsQueryArg ]);
const { items, isLoading, isFetching } = useGetItemsQuery('itemsList', {
selectFromResult: ({ data }) => ({
items: data && entitySelectors.selectAll(data)
}),
})
} |
Beta Was this translation helpful? Give feedback.
don't pass a selector to getSelectors and call that resulting selector in selectFromResult.
If you're calling it from selectFromResult, you need a selector that expects to only receive the entity state - i.e. call getSelectors() with no arguments.