In my below React component I am fetching more items using Apollo Client fetchMore
function.
What is exactly the purpose of using React useCallback hook here? And is it good idea to use that in this case?
const Carousel = ({ data: blockData, metadata }: Props) => {
const { data, fetchMore, networkStatus } = useQuery<
GqlRes,
>(GET_NODES, {
variables: {
offset
limit
},
errorPolicy: 'all',
notifyOnNetworkStatusChange: true,
ssr: false,
});
const fetchNextPage = useCallback(() => {
return fetchMore({
variables: {
offset: data?.getNodes.nodes.length,
},
});
}, [data, fetchMore]);
const items =
data?.getNodes.nodes
.map((node) => {
return {
id: node.id,
title: node.title,
};
}) || [];
return (
<Carousel
items={items}
onEnd={() => {
if (data?.getNodes.pager?.hasNextPage) {
fetchNextPage();
}
}}
/>
)
};
export default Carousel;