Skip to content

fix: guard against undefined global state in createCacheHelper#4224

Open
veeceey wants to merge 1 commit intovercel:mainfrom
veeceey:feat/issue-2986-rn-new-arch
Open

fix: guard against undefined global state in createCacheHelper#4224
veeceey wants to merge 1 commit intovercel:mainfrom
veeceey:feat/issue-2986-rn-new-arch

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 23, 2026

When using SWR with React Native's New Architecture (bridgeless mode), SWRGlobalState.get(cache) can return undefined during early renders before the provider has fully initialized. This causes a crash when createCacheHelper tries to destructure state[5] and state[6] from the undefined value.

This adds a simple null guard:

  • The setter becomes a no-op when state is undefined (the update will happen once the provider initializes properly)
  • The subscriber falls back to a function that returns a no-op unsubscribe handler

Tested locally - all existing unit and integration tests still pass.

Closes #2986

@veeceey
Copy link
Author

veeceey commented Feb 23, 2026

Test results

All existing tests pass:

$ pnpm test -- --testPathPattern "test/unit" --no-coverage
Test Suites: 3 passed, 3 total
Tests:       9 passed, 9 total

$ pnpm test -- --testPathPattern "test/use-swr-cache" --no-coverage
Test Suites: 1 passed, 1 total
Tests:       20 passed, 20 total

$ pnpm test -- --testPathPattern "test/use-swr-remote-mutation" --no-coverage
Test Suites: 1 passed, 1 total
Tests:       33 passed, 33 total

Type checks also pass (tsc --noEmit via pre-commit hook).

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 23, 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.

SWRGlobalState.get(cache) can return undefined in React Native New
Architecture during initial renders before the provider has been
initialized. This causes a crash when accessing state[5] or state[6].

Add a null check so the setter becomes a no-op and the subscriber
falls back to a no-op function when global state hasn't been set up yet.

Closes vercel#2986
@veeceey veeceey force-pushed the feat/issue-2986-rn-new-arch branch from 4e80948 to 66b6676 Compare March 10, 2026 13:09
@veeceey
Copy link
Author

veeceey commented Mar 12, 2026

hi, just wondering if this could get a look. let me know if anything needs updating!

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.

useSWRMutation is not working with React Native New Architecture.

1 participant