admin管理员组文章数量:1334704
I was trying to perform the infinite-query using a json-server data. I have tried this with react-query, not tanstack query. It is not throwing any error, but showing the same value again and again. I have checked for pageParams working or not, it is increasing properly. Infact the button disabled thing also working fine, but data is not changing, it is not appending the new data. Can anyone please help?
The code is:
import { useInfiniteQuery } from "react-query";
import axios from "axios";
import React from "react";
const fetchUsers = ({ pageParam = 1 }) =>
axios.get(`http://localhost:1000/users?_page=${pageParam}&_limit=2`);
const InfiniteQuery = () => {
const {
isLoading,
isError,
error,
data,
hasNextPage,
fetchNextPage,
isFetching,
isFetchingNextPage,
} = useInfiniteQuery(["users"], fetchUsers, {
getNextPageParam: (_lastPage, pages) => {
if (pages.length < 3) {
return pages.length + 1;
} else {
return undefined;
}
},
});
console.log("data", data);
if (isLoading) {
return <h2>Loading...</h2>;
}
if (isError) {
return <h2>{error.message}</h2>;
}
return (
<div>
{data?.pages.map((usrGrp, index) => (
<React.Fragment key={index}>
{usrGrp?.data.map((usr) => (
<div key={usr.id}>
<h2>{usr.name}</h2>
<hr />
</div>
))}
</React.Fragment>
))}
<div>
<button disabled={!hasNextPage} onClick={fetchNextPage}>
Load more
</button>
</div>
<div>{isFetching && !isFetchingNextPage ? "Fetching..." : null}</div>
</div>
);
};
export default InfiniteQuery;
And the json data is:
{
"users": [
{
"id": "1",
"name": "John Doe"
},
{
"id": "2",
"name": "Mary James"
},
{
"id": "3",
"name": "Tom Brown"
},
{
"id": "4",
"name": "Ron Watson"
},
{
"id": "5",
"name": "Luna Paul"
},
{
"id": "6",
"name": "Richard Harris"
}
]
}
I was trying to perform the infinite-query using a json-server data. I have tried this with react-query, not tanstack query. It is not throwing any error, but showing the same value again and again. I have checked for pageParams working or not, it is increasing properly. Infact the button disabled thing also working fine, but data is not changing, it is not appending the new data. Can anyone please help?
The code is:
import { useInfiniteQuery } from "react-query";
import axios from "axios";
import React from "react";
const fetchUsers = ({ pageParam = 1 }) =>
axios.get(`http://localhost:1000/users?_page=${pageParam}&_limit=2`);
const InfiniteQuery = () => {
const {
isLoading,
isError,
error,
data,
hasNextPage,
fetchNextPage,
isFetching,
isFetchingNextPage,
} = useInfiniteQuery(["users"], fetchUsers, {
getNextPageParam: (_lastPage, pages) => {
if (pages.length < 3) {
return pages.length + 1;
} else {
return undefined;
}
},
});
console.log("data", data);
if (isLoading) {
return <h2>Loading...</h2>;
}
if (isError) {
return <h2>{error.message}</h2>;
}
return (
<div>
{data?.pages.map((usrGrp, index) => (
<React.Fragment key={index}>
{usrGrp?.data.map((usr) => (
<div key={usr.id}>
<h2>{usr.name}</h2>
<hr />
</div>
))}
</React.Fragment>
))}
<div>
<button disabled={!hasNextPage} onClick={fetchNextPage}>
Load more
</button>
</div>
<div>{isFetching && !isFetchingNextPage ? "Fetching..." : null}</div>
</div>
);
};
export default InfiniteQuery;
And the json data is:
{
"users": [
{
"id": "1",
"name": "John Doe"
},
{
"id": "2",
"name": "Mary James"
},
{
"id": "3",
"name": "Tom Brown"
},
{
"id": "4",
"name": "Ron Watson"
},
{
"id": "5",
"name": "Luna Paul"
},
{
"id": "6",
"name": "Richard Harris"
}
]
}
Share
Improve this question
asked Nov 20, 2024 at 12:14
Soumi GhoshSoumi Ghosh
1391 silver badge11 bronze badges
1 Answer
Reset to default 0I have finally found the solution. I have changed axios.get(
http://localhost:1000/users?_page=${pageParam}&_limit=2);
this to : axios.get(http://localhost:1000/users?_page=${pageParam}&_per_page=2
);
The response is a little complex, I have used map within a map to display data, But it is working fine.
The return part is :
return (
<div>
{
data?.pages?.map((page,index)=>(
<React.Fragment key={index}>
{
page.data.data.map((usr) => (
<div key={usr.id}>
<h2>{usr.name}</h2>
<hr />
</div>
))
}
</React.Fragment>
))
}
<div>
<button disabled={!hasNextPage} onClick={fetchNextPage}>
Load more
</button>
</div>
<div>{isFetching && !isFetchingNextPage ? "Fetching..." : null}</div>
</div>
);
本文标签: reactjsInfiniteQuery is not working properly using reactqueryStack Overflow
版权声明:本文标题:reactjs - InfiniteQuery is not working properly using react-query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358177a2459832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论