query: How to use useInfiniteQuery with custom props
Let’s picture situation where we have filters with infinite scroll data view.
So I have have code like this:
// api
const queryPosts = async (props = {}) => {
const { sort, page } = props
const res = await axios.get(`/posts?sort=${sort}&page=${page}`)
return {
items: res.data,
page,
}
}
// Posts
const [sort, setSort] = React.useState('views')
const payload = useInfiniteQuery(
['infinite-posts', { sort, page: 1 }],
queryPosts,
{
getFetchMore: lastGroup => {
const { items, group } = lastGroup
if (items.length) {
return { page: page + 1, sort }
}
return false
}
}
)
At first it triggers correct, but when I call () => fetchMore() sort is disappeared, and page remains as initial.
Maybe I’m doing something wrong?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 15
Hello, I think this could be better documented. I also had trouble using
useInfiniteQuerywith an additional parameter until I found this issue.The issue here is that you are not mapping up your query key to your query function’s arguments properly. Each item in the query key is passed to the query function, including
infinite-postsin your case. The next issue is that you’re not accounting that the result fromgetFetchMoreis passed as an optional parameter to your query function, which is why on the first request, you must provide a default value.😃 You’re welcome! Thanks for using it! You should consider writing a blog post on that thought. It sounds like a great prompt: “React Query: The missing abstraction for hooks and data fetching”
Following worked for me
@rsarai maybe this isn’t a good way to to send the props to the service since it forces we to write the API request with some boilerplate args or the args as object, probably breaking the re-usability of that service outside of
react-queryon some cases. Another point is that as I see on the docs neither the useInfiniteQuery or useQuery acceptsgetFetchMoremethod on react-query v3.But as I found here, on the migration guide have a really useful doc about
useInfiniteQueryargs.Basically we just need to call the service through a function like this:
with this we can treat the
pageParamsisolated from the service and, sending an object tofetchNextPagewe get the new props of the function, it becomes really useful if we create a custom hook as below. But is good to say, I have to work on a more readable way to write this, but at least IMO this is better than change the way we write our services here.BTW I’m pretty new to
react-queryand I’m probably missing something, @tannerlinsley what you think about that approach?For anyone facing a facet of this issue, the following worked for me:
hey @Alecell, greetings, noticed we share the same timezone. You are 100% correct about the
getFetchMore, on my project I’m actually usinggetNextPageParam(🙈).I was trying to go for different types of query keys. I’m also new to
react-queryso I’m happy to hear other comments.Unfortunately I have the same issue, is not clear from the docs how to use it 😦