fix: extra render when changing to new key with useSWRImmutable#4209
fix: extra render when changing to new key with useSWRImmutable#4209
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Pull request overview
This PR fixes an extra render issue that occurred when changing to a new key with useSWRImmutable. The problem was that the loading states (isLoading and isValidating) were not being set correctly when switching to a new key that had no cached data.
Changes:
- Moved
shouldStartRequestlogic insidegetSelectedCacheto accesssnapshot.data, enabling proper detection of uncached keys - Simplified
isValidatingandisLoadingstate determination by removing redundantdefaultValidatingStatevariable - Added comprehensive test to verify correct loading states and render count when switching keys
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/use-swr-immutable.test.tsx | Added new test with Profiler to verify exactly 4 renders occur when switching keys, and that loading states are correctly set to true for new keys |
| src/index/use-swr.ts | Fixed loading state logic by moving shouldStartRequest inside getSelectedCache to access snapshot data, and simplified state computation by removing redundant checks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
close #4112 #2930