Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In useSWRInfinite i want to get next page url along with data. #59

Closed
varunkumargande opened this issue Dec 17, 2020 · 5 comments
Closed

Comments

@varunkumargande
Copy link

let's assume my data is in this format
{
"count": 4,
"next": "http://localhost:8000/api/?page=2&id=102&paginate_by=2",
"previous": null,
"results": [
{
"id": 153,
"name": "pre workshop"
},
{
"id": 154,
"name": "workshop"
}
]
}

now if write code in this way, I don't have the option to store next page URL
const fetcher = url => axios.get(url).then(res => res.data);
const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite(
index =>
https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${index + 1},
fetcher
);

I want to store data.results in data array and also store next page URL.
how can I achieve that

@varunkumargande
Copy link
Author

@shuding please help me out with this issue

I want my fetcher to be like this with arguments

const fetcher = url => axios.get(url).then(res =>
{
data = res.data.results;
next = res.data.next;
}
);

@shuding
Copy link
Member

shuding commented Dec 17, 2020

Please read the documentation: https://swr.vercel.app/docs/pagination#example-2-cursor-or-offset-based-paginated-api

There's a second argument for the getKey function: previousPageData. In your case just return the next endpoint URL based on that.

@varunkumargande
Copy link
Author

varunkumargande commented Dec 17, 2020

Please read the documentation: https://swr.vercel.app/docs/pagination#example-2-cursor-or-offset-based-paginated-api

There's a second argument for the getKey function: previousPageData. In your case just return the next endpoint URL based on that.

if this is the data format
GET /users?cursor=123&limit=10
{
data: [
{ name: 'Alice' },
{ name: 'Bob' },
{ name: 'Cathy' },
...
],
nextCursor: 456
}

I want to concatenate data of first-page results with second-page results and so on ...

but in doc you are using map inside map like below.

{data.map((users, index) => {
// data is an array of each page's API response.
return users.map(user =>

{user.name}
)
})}
is map inside map is a right way to code?

and also can I pass URL via getKey like getKey(URL) ?

@shuding , @max please help me out with this
thanks in advance

@shuding
Copy link
Member

shuding commented Dec 17, 2020

@varunkumargande if you want to concatenate the data, you can do it after the hook call:

const { data } = useSWRInfinite(...)
const mergedData = data ? [].concat(data.map(item => item.results)) : []

@varunkumargande
Copy link
Author

@varunkumargande if you want to concatenate the data, you can do it after the hook call:

const { data } = useSWRInfinite(...)
const mergedData = data ? [].concat(data.map(item => item.results)) : []

@shuding thanks, fixed my usecase

shuding added a commit that referenced this issue Jun 6, 2021
* feat: Add japanese translate (#47)

* doc: translate "Global Configuration" page into Japanese (#48)

* doc: translate "Data-Fetching" page into Japanese (#50)

* docs: Add Japanese markdown or mdx files (#54)

* doc: translate "API Options" page into Japanese (#56)

* doc: translate "Top" page into Japanese (#55)

* doc: translate "Conditional Fetching" page into Japanese (#57)

* doc: translate "mutation" page into Japanese (#58)

* doc: translate "Suspense" page into Japanese (#60)

* doc: translate "Change Log" page into Japanese (#62)

* doc: translate "Prefetching Data" page into Japanese (#59)

* doc: translate "Automatic Revalidation" page into Japanese (#63)

* doc: translate "Getting Started" page into Japanese (#61)

* doc: translate "Arguments" page into Japanese (#64)

* doc: translate the swr title into Japanese (#71)

* doc: translate "Advanced Performance" page into Japanese (#65)

* doc: translate "Error Handling" page into Japanese (#69)

* doc: translate the side menu into Japanese (#72)

* doc: translate "Usage with Next.js" page into Japanese (#70)

* doc: translate "pagination" page into Japanese(#73)

* doc: proofread "Top" page (#77)

* doc: proofread "Global Configuration" page (#81)

* doc: proofread "Getting Started" page (#79)

* doc: proofread "Error Handling" page (#83)

* doc: proofread "Conditional Fetching" page (#85)

* doc: proofread "Pagination" page (#87)

* doc: proofread "Usage with Next.js" page (#89)

* docs: proofread the doc pages (#91)

* fix: resolve conflicts (#93)

* Fix code example in data-fetching.zh-CN.mdx (#90)

* Add license (#93)

* Add license

* Update license.md

Co-authored-by: Paco <[email protected]>

Co-authored-by: Shu Ding <[email protected]>
Co-authored-by: Paco <[email protected]>

* update perf page (#92)

* I18n: Spanish (#96)

* feat: added support for Spanish language

* doc(language): now the footer text is on Spanish

* docs(language): I18: Snpanish

* docs(language): Added metadata Spanish

* docs(language): I18 index spanish page

* docs(language): I18 Getting started to spanish

* docs(language): I18 Change log page to Spanish

* feat(I18): Add metadata related to the example to Spanish

* docs(I18): Basic use to the Spanish

* docs(I18): Auth page to Spanish

* docs(I18): Handleing error page to Spanish

* docs(I18): Inifinite loading page to Spanish

* feat(I18-spanish): added metadata related with advances section

* docs(I18): Translated to Spanish

* docs(I18): Added metadata related with documentation section

* refactor: fix typo

* docs(I18): Option translated to Spanish

* docs(I18): Configuration global page translated to Spanish

* docs(I18): Data fetching page translated to Spanish

* docs:(I18): Error handling page translated to Spanish

* refactor: Fixy typo

* docs(I18): Automatic revalidation page translated to Spanish

* docs(I18): Conditional data fetching page translated to Spanish

* docs(I18): Arguments page translated to Spanish

* docs(I18) Mutation page translated to Spanish

* docs(I18): Pagination page translated to Spanish

* docs(I18): Prefetching page translated to Spanish

* docs(I18): Usage with Next.js page translated to Spanish

* docs(I18): Suspense page translated to Spanish

* refactor: performance page

* docs(readme): Improving credits and add one more. (#97)

* i18n: add Japanese

Co-authored-by: Fang Lu <[email protected]>
Co-authored-by: Joe Cohen <[email protected]>
Co-authored-by: Shu Ding <[email protected]>
Co-authored-by: Paco <[email protected]>
Co-authored-by: Markoz Peña <[email protected]>

* Revert "fix: resolve conflicts (#93)"

This reverts commit b29c7b8.

* feat(i18n): add Japanese (#95)

* doc: update translation of "Performance" page (#98)

* doc: add Japanese translators to README.md (#99)

* doc: fix the feature section of top page (#101)

Co-authored-by: Tomohiro (Tomo) SHIOYA <[email protected]>
Co-authored-by: Fang Lu <[email protected]>
Co-authored-by: Joe Cohen <[email protected]>
Co-authored-by: Shu Ding <[email protected]>
Co-authored-by: Paco <[email protected]>
Co-authored-by: Markoz Peña <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants