You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a very specific use case where I have not yet found a solution with useSWR. Basically I have a paged search result, and when the user tries to open a page that does not exist, the data should be resetted to the last existing page. I've implemented this similar to this simplified code:
interface SearchResult {
elements: unknown[]
pages: number
// count of elements on ALL pages of the search result
count: number
}
...
const [filters, setFilters] = useState([page, sortOrder, searchText])
const { data, error, mutate, isValidating } = useSWR<SearchResult, Error>(['search', ...filters])
// when a page after the last page has been requested, reset to the last page
if (!isValidating && data && data?.count > 0 && data?.elements.length === 0) {
setFilters(([page, ...remainingFilters]) => [data.pages, ...remainingFilters])
}
This mostly works except for one case:
I start with 51 elements while the page size is 50
I delete the last element
Automatically by the code in the last if, I get thrown back to page 1 because this is the last page
I create a new element in its own flow, but I don't mutate my search result since I would need to do it for all permutations of the filters which is impossible - at the end of the flow I end up on page 1 where the new element shows up
I try to go to page 2
Now the problematic part happens. Page 2 returns the stale response from after step 2 which has empty elements. This means the code in the if kicks in and throws me back to page 1, even before the response can be revalidated. Curiously isValidating is false - is this a bug?
So to sum up, when I click the page 2 button, nothing happens for the user, only the second click gets the user to page 2 because the first attempt revalidated the second page.
Ok, so 2 questions:
Is this a bug with isValidating?
Are there other suggestions on how to solve my issue? Note that I want to avoid to preload pages because of DB load concerns.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a very specific use case where I have not yet found a solution with useSWR. Basically I have a paged search result, and when the user tries to open a page that does not exist, the data should be resetted to the last existing page. I've implemented this similar to this simplified code:
This mostly works except for one case:
if
, I get thrown back to page 1 because this is the last pagemutate
my search result since I would need to do it for all permutations of the filters which is impossible - at the end of the flow I end up on page 1 where the new element shows upNow the problematic part happens. Page 2 returns the stale response from after step 2 which has empty
elements
. This means the code in theif
kicks in and throws me back to page 1, even before the response can be revalidated. CuriouslyisValidating
isfalse
- is this a bug?So to sum up, when I click the
page 2
button, nothing happens for the user, only the second click gets the user to page 2 because the first attempt revalidated the second page.Ok, so 2 questions:
isValidating
?Beta Was this translation helpful? Give feedback.
All reactions