Skip to content

fix(store): bind get and subscribe on ReadonlyStore - #424

Open
oyobbeb wants to merge 1 commit into
TanStack:mainfrom
oyobbeb:fix/readonly-store-bind
Open

oyobbeb wants to merge 1 commit into
TanStack:mainfrom
oyobbeb:fix/readonly-store-bind

Conversation

@oyobbeb

@oyobbeb oyobbeb commented Nov 24, 2026 •

Copy link
Copy Markdown

🎯 Changes

Store binds get, setState and subscribe in its constructor so they can be destructured, but ReadonlyStore does not. A derived store from createStore(() => ...) is a ReadonlyStore, so this throws:

const count = createStore(1)
const { get, subscribe } = createStore(() => count.state * 2)

get() // TypeError: Cannot read properties of undefined (reading 'state')
subscribe(() => {}) // TypeError: Cannot read properties of undefined (reading 'atom')

This binds get and subscribe on ReadonlyStore the same way, and adds a test for a destructured derived store.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Destructured get and subscribe methods from read-only stores now work correctly, matching the behavior available on regular stores.
    • Subscriptions continue to receive updated derived values when their dependencies change, and can be stopped using the returned unsubscribe function.

Store binds its methods so they can be destructured, but ReadonlyStore
did not, so a destructured get or subscribe from a derived store threw
because `this` was undefined.
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

ReadonlyStore now binds get and subscribe to its instance. A derived-store test checks destructured method calls and subscription cleanup. A changeset records the patch release.

Changes: ReadonlyStore method binding

Layer / File(s) Summary
Bind and verify ReadonlyStore methods
packages/store/src/store.ts, packages/store/tests/derived.test.ts, .changeset/readonly-store-bind.md
The constructor binds get and subscribe to the instance. The derived-store test checks the initial value, notification after a dependency update, and invocation of unsubscribe. The changeset records a patch release.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: davidkpiano

Merge Risk: Minimal · up to 2ff53

This patch makes destructured get and subscribe work on derived stores, as they already do on Store. It carries no identified merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: binding get and subscribe on ReadonlyStore. It matches the raw summary, the PR objectives, and the store.ts change. It uses the conventional fix(store) prefix and is s…
Description check Passed The description covers the Changes section with the motivation, a reproduction example, and the fix. The checklist is filled in. Both release-impact boxes are marked, but only the changeset box is che…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: binding get and subscribe on ReadonlyStore. It matches the raw summary, the PR objectives, and the store.ts change. It uses the conventional fix(store) prefix and is specific.

Full details: Description check

Explanation

The description covers the Changes section with the motivation, a reproduction example, and the fix. The checklist is filled in. Both release-impact boxes are marked, but only the changeset box is checked, which is consistent with the changeset file in the PR. The description follows the repository template.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3ee69b9 and 2ff535b.

Files selected for processing: 3

  • .changeset/readonly-store-bind.md
  • packages/store/src/store.ts
  • packages/store/tests/derived.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2ff535b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/store Patch
@tanstack/angular-store Patch
@tanstack/lit-store Patch
@tanstack/octane-store Patch
@tanstack/preact-store Patch
@tanstack/react-store Patch
@tanstack/solid-store Patch
@tanstack/svelte-store Patch
@tanstack/vue-store Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@oyobbeb
oyobbeb marked this pull request as ready for review November 26, 2026 03:12
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.

1 participant