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

feat: add isLoadingMore option for useSWRInfinite #2728

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions examples/infinite/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ export default function App() {
const [repo, setRepo] = useState('reactjs/react-a11y')
const [val, setVal] = useState(repo)

const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite(
(index) =>
`https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${
index + 1
}`,
fetch
)
const { data, mutate, size, setSize, isValidating, isLoadingMore } =
useSWRInfinite(
index =>
`https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${
index + 1
}`,
fetch
)

const issues = data ? [].concat(...data) : []
const isLoadingInitialData = !data && !error
const isLoadingMore =
isLoadingInitialData ||
(size > 0 && data && typeof data[size - 1] === 'undefined')
const isEmpty = data?.[0]?.length === 0
const isReachingEnd =
isEmpty || (data && data[data.length - 1]?.length < PAGE_SIZE)
Expand All @@ -31,7 +28,7 @@ export default function App() {
<div style={{ fontFamily: 'sans-serif' }}>
<input
value={val}
onChange={(e) => setVal(e.target.value)}
onChange={e => setVal(e.target.value)}
placeholder="reactjs/react-a11y"
/>
<button
Expand Down Expand Up @@ -63,7 +60,7 @@ export default function App() {
</button>
</p>
{isEmpty ? <p>Yay, no issues found.</p> : null}
{issues.map((issue) => {
{issues.map(issue => {
return (
<p key={issue.id} style={{ margin: '6px 0' }}>
- {issue.title}
Expand Down
10 changes: 9 additions & 1 deletion infinite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
[infiniteKey, cache, mutate, resolvePageSize]
)

// Based on https://swr.vercel.app/examples/infinite-loading
const isLoadingMore =
swr.isLoading ||
(resolvePageSize() > 0 &&
swr.data &&
typeof swr.data[resolvePageSize() - 1] === 'undefined')

// Use getter functions to avoid unnecessary re-renders caused by triggering
// all the getters of the returned swr object.
return {
Expand All @@ -324,7 +331,8 @@ export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
},
get isLoading() {
return swr.isLoading
}
},
isLoadingMore
}
}) as unknown as Middleware

Expand Down
1 change: 1 addition & 0 deletions infinite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface SWRInfiniteResponse<Data = any, Error = any>
setSize: (
size: number | ((_size: number) => number)
) => Promise<Data[] | undefined>
isLoadingMore: boolean | undefined
}

export interface SWRInfiniteHook {
Expand Down
Loading