如何使用React调用多个页面调用API
我确实有一个API需要获取我的React应用程序。挑战是我有一个示例的API中不存在总页数。另外,每个API呼叫限制一次仅获得1000个记录。我确实有一个应该按页面上的按钮,但是我认为它不应该按照自己的作用,在获取了所有数据后,它仍然会进行API调用。我的问题是:
我如何获取API,因此即使有一个限制,所有5000个记录都将填充
我如何使按钮一次fetch api在一次不会消失的1000个记录中,因为该按钮是故障的,当我单击它,它将获取1000,但消失了。
const perpage = 1000; const [page,setPage] = usestate([]); const [用户列表,setUserList] = usestate([]); const [加载,setloading] = usestate(false); const [load,setload] = usestate([]); useeffect(()=> { const getUserList =()=> { setloading(true); 拿来( `/forms?page = $ {page}& count = $ {perpage}`, {标题} ) 然后。 。然后((res)=> { //setTotalPages(Res.total_pages); setload(res.elements); setUserList(res.elements); setPage(res.page); if(res.elements.length === 0){ console.log(“数组为空”); 警报(“空”); setloading(false); } console.log(res); }); }; getUserList(); }, [页]); 返回 ( < div className =“ app”> {userList.map(((x,i)=> { 返回 ( < div key = {i} className =“ box”> < div className =“ name”> {x.name + 1}</div> </div> ); })}} < button className =“ btn-load-more” onclick = {()=> setPage(page + 1)}> {加载中 ? “加载...”:“加载更多”} </butt> </div> );
{ “元素”:[{{ “ type”:“ landingpage”, “ CurrentStatus”:“草稿”, “ id”:“ 21”, “创建”:“ 1424811452”, “创建”:“ 5”, “深度”:“最小”, “ folderid”:“ 3655”, “名称”:“默认着陆页”, “权限”:[“检索”,“ setSecurity”,“ delete”,“ update”],, “更新”:“ 1424811452”, “更新”:“ 5”, “排除Fromauthentication”:“ false”, “ htmlcontent”:{ “ type”:“ structuredhtmlcontent”, “ contentsource”:“编辑器” },, “相对路径”:“/default_lp” },{ “ type”:“ landingpage”, “ CurrentStatus”:“草稿”, “ id”:“ 22”, “创建”:“ 1432583568”, “创建”:“ 9”, “深度”:“最小”, “ folderid”:“ 3655”, “名称”:“测试着陆页”, “权限”:[“检索”,“ setSecurity”,“ delete”,“ update”],, “ UpdatedAt”:“ 1432583653”, “更新”:“ 9”, “排除Fromauthentication”:“ false”, “ htmlcontent”:{ “ type”:“ rawhtmlcontent”, “ contentsource”:“上传” },, “相对路径”:“/test_landing_page” }], “页面”:1, “ pageize”:1000, “总计”:5000 }
`
I do have an API that I need to fetch into my react app. The challenge is the number of total pages doesn't exist in the API where I have the example. Also, every API call limit only gets 1000 records at a time. I do have a button that is supposed to go page by page, but I don't think it's working as it should, after all the data is fetched, it would still make an API call. My question is :
How do I fetch the API so all the 5000 records will populate even though there's a limit
How do I make the button to fetch API to load 1000 records at a time that doesn't disappear, because the button is buggy, when I click on it, it'll fetch 1000, but it disappears.
const perPage = 1000; const [page, setPage] = useState([]); const [userList, setUserList] = useState([]); const [loading, setLoading] = useState(false); const [load, setload] = useState([]); useEffect(() => { const getUserList = () => { setLoading(true); fetch( `/forms?page=${page}&count=${perPage}`, { headers } ) .then((res) => res.json()) .then((res) => { //setTotalPages(res.total_pages); setload(res.elements); setUserList(res.elements); setPage(res.page); if (res.elements.length === 0) { console.log("array is empty"); alert("empty"); setLoading(false); } console.log(res); }); }; getUserList(); }, [page]); return ( <div className="App"> {userList.map((x, i) => { return ( <div key={i} className="box"> <div className="name">{x.name + 1} </div> </div> ); })} <button className="btn-load-more" onClick={() => setPage(page + 1)}> {loading ? "Loading..." : "Load More"} </button> </div> );
{ "elements": [{ "type": "LandingPage", "currentStatus": "Draft", "id": "21", "createdAt": "1424811452", "createdBy": "5", "depth": "minimal", "folderId": "3655", "name": "Default Landing Page", "permissions": ["Retrieve", "SetSecurity", "Delete", "Update"], "updatedAt": "1424811452", "updatedBy": "5", "excludeFromAuthentication": "false", "htmlContent": { "type": "StructuredHtmlContent", "contentSource": "editor" }, "relativePath": "/Default_LP" }, { "type": "LandingPage", "currentStatus": "Draft", "id": "22", "createdAt": "1432583568", "createdBy": "9", "depth": "minimal", "folderId": "3655", "name": "Test Landing Page", "permissions": ["Retrieve", "SetSecurity", "Delete", "Update"], "updatedAt": "1432583653", "updatedBy": "9", "excludeFromAuthentication": "false", "htmlContent": { "type": "RawHtmlContent", "contentSource": "upload" }, "relativePath": "/Test_Landing_Page" }], "page": 1, "pageSize": 1000, "total": 5000 }
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
公平地说,如果您知道API将始终有5个总页面并且不会阻止多个请求,那么我会以下面的方式进行:
Update
我打开了一个沙盒并使用了Randomuser.me,它具有分页的端点,并使其与您的方法一起使用,因此您在setload()中添加的值可能会出现问题。我唯一要做的是为React 18 UseFect()double渲染添加一个abortController。在此示例中,我没有下一页号或总数
To be fair, if you know that the API will always have 5 total pages and it doesn't block multiple requests, I would do it in the following way:
UPDATE
I opened a sandbox and used randomuser.me which has paginated endpoints and made it work with your approach so there is probably something going wrong with the value you add in setload(). The only extra thing I did is to add an AbortController for React 18 useEffect() double render. In this example I don't have the next page number or the total but if you have them you can calculate the final request with something like