T
TanStack3y ago
afraid-scarlet

Is using 'useCallback' inside 'select' option a good pattern?

I saw somewhere that useCallback should be used inside an expensive select
const { data } = useQuery({
queryKey: ["myKey", { id }],
queryFn: myFN,
select: useCallback((data) => data.slice(0, 9), [id]),
});
const { data } = useQuery({
queryKey: ["myKey", { id }],
queryFn: myFN,
select: useCallback((data) => data.slice(0, 9), [id]),
});
is that still the a good practice?
2 Replies
unwilling-turquoise
unwilling-turquoise3y ago
1) yes but slicing an array is not expensive 2) you're not using id inside the function so why would it be a dependency?
afraid-scarlet
afraid-scarletOP3y ago
1) was just for the example here 2) same (my mistake) Thanks

Did you find this page helpful?