简体   繁体   English

如何解决反应项目中的分页问题

[英]how to fix pagination issue in react project

i have an react project which is listing some news from an api .我有一个反应项目,其中列出了来自 api 的一些新闻。 in that api each page gives me 10 newses .在那个 api 中,每个页面都给了我 10 个新闻。 for example when i give input 0 to the api , it will gives me 1-10 news .例如,当我给 api 输入 0 时,它会给我 1-10 个新闻。 when i give input 1 it will gives me 11-20 news etc ..当我输入 1 时,它会给我 11-20 条新闻等。

in my case i will change the apis input when the scroll bar touch the end of the Screen InnerHeight .在我的情况下,当滚动条触摸 Screen InnerHeight 的末尾时,我将更改 api 输入。 also i will fetch that api again .我也将再次获取该 API。 then the new datas i will append to the oldest data array .然后我将新数据附加到最旧的数据数组中。

for example :- when the page is open the input is 0 , so the api will gives first 10 news .例如:-当页面打开时输入为 0,因此 api 将给出前 10 个消息。 so i stored in that in a state , then the user scroll in that page , when the scrollbar touch the end i changed the input to 1 and fetch the api again .所以我将其存储在一个状态中,然后用户在该页面中滚动,当滚动条触摸末尾时,我将输入更改为 1 并再次获取 api。 so it will gives the 11-20 news .所以它会给出 11-20 的消息。 i will append to that in that oldest state .我将在那个最古老的状态中附加它。 so now in that state total 20 news.所以现在在那个状态总共有 20 条新闻。

also each news has a detail news page .when the user click the news it will be redirect to the detail news page .每个新闻都有一个详细新闻页面。当用户点击新闻时,它将被重定向到详细新闻页面。 the problem is when the user clicks back button from the detail news page , it will redirect to the all news page , but it will fetch again , then the api input will be 0 , so the user cannot see the last time visited news .问题是当用户从详细新闻页面单击返回按钮时,它会重定向到所有新闻页面,但它会再次获取,然后api输入将为0,因此用户看不到最后一次访问的新闻。 it will be on the top of window .它将在窗口的顶部。

for example when the user clicks 56 news , then it will redirect to the 56th detail news page.例如,当用户点击 56 news 时,它将重定向到第 56 个详细新闻页面。 but when click back button it will be on the 0th news position .但是当单击返回按钮时,它将位于第 0 个新闻位置。

how can i fix that .我该如何解决。 how to get 56th news position when the user clicks back button .当用户单击返回按钮时如何获得第 56 个新闻位置。

sorry for my bad English对不起,我的英语不好

the problem is: your news index page (or component) does not have access to the last news your user saw, it have access to it's inner state, but when the user goes to detailed news page, that state will be destroyed.问题是:您的新闻索引页面(或组件)无法访问您的用户看到的最后一条新闻,它可以访问它的内部状态,但是当用户转到详细的新闻页面时,该状态将被破坏。

you can have two approaches to achieve what you want:你可以有两种方法来实现你想要的:

use a global state manager like redux:使用像 redux 这样的全局状态管理器:

in redux you can store a global state, that all of your components have access to.在 redux 中,您可以存储所有组件都可以访问的全局状态。

each time your user visits the next news pagination, you update your redux store, and when he comes back from a detailed news page, you can see how many news he has seen, and call your api for example accordingly每次你的用户访问下一个新闻分页时,你都会更新你的 redux store,当他从详细的新闻页面回来时,你可以看到他看到了多少新闻,并相应地调用你的 api

another aproach is to use your router for storing user data:另一种方法是使用您的路由器来存储用户数据:

when your user visits your api with index 1 you update your url accordingly: for ex: /news for main idnex page and when the user sees another one, you update it with /news?index=1当您的用户使用索引 1 访问您的 api 时,您会相应地更新您的 url:例如:/news 用于主 idnex 页面,当用户看到另一个页面时,您使用 /news?index=1 更新它

in this situation if the user hits the back button, it will be redirected to the last seen page, and you can read the router in your component to determine what api call your user last visited and load the needed data for him/her在这种情况下,如果用户点击后退按钮,它将被重定向到最后看到的页面,您可以读取组件中的路由器以确定您的用户最后访问的 api 调用并为他/她加载所需的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM