Skip to content

fix: extra render when changing to new key with useSWRImmutable#4209

Merged
promer94 merged 5 commits intomainfrom
fix/immutable-init-loading
Jan 21, 2026
Merged

fix: extra render when changing to new key with useSWRImmutable#4209
promer94 merged 5 commits intomainfrom
fix/immutable-init-loading

Conversation

@promer94
Copy link
Copy Markdown
Collaborator

@promer94 promer94 commented Jan 10, 2026

close #4112 #2930

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Jan 10, 2026

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 shouldStartRequest logic inside getSelectedCache to access snapshot.data, enabling proper detection of uncached keys
  • Simplified isValidating and isLoading state determination by removing redundant defaultValidatingState variable
  • 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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@promer94 promer94 requested review from huozhi and shuding and removed request for huozhi and shuding January 19, 2026 10:41
Copy link
Copy Markdown
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you @promer94! ❤️

@promer94 promer94 merged commit b75456a into main Jan 21, 2026
6 checks passed
@promer94 promer94 deleted the fix/immutable-init-loading branch January 21, 2026 04:26
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

Successfully merging this pull request may close these issues.

SWR state has undocumented and unexpected values on key change

3 participants