Skip to content

feat: new react hooks#306

Open
KevinVandy wants to merge 11 commits intomainfrom
hooks
Open

feat: new react hooks#306
KevinVandy wants to merge 11 commits intomainfrom
hooks

Conversation

@KevinVandy
Copy link
Copy Markdown
Member

@KevinVandy KevinVandy commented Apr 11, 2026

🎯 Changes

  • Depecated useStore in favor of useSelector

  • useValue(source, options?)
    Read the current value from an atom or the full state from a store.

  • useSetValue(source)
    Get a stable setter for a writable atom or store while preserving that source’s native setter behavior.

  • useAtom(atom, options?)
    Keep the atom tuple API for reading and writing writable atoms as [value, setValue].

  • useSelector(source, selector, options?)
    Keep the unified selector hook for deriving slices from atoms or stores.

  • useCreateAtom(initialValue, options?)
    Create a stable local atom instance for the lifetime of a component.

  • useCreateStore(initialValue)
    Create a stable local store instance for the lifetime of a component.

  • createStoreContext()
    Create a typed React context for passing grouped atoms and stores through a subtree.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 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

  • New Features

    • New React hooks: useSelector, useValue, useSetValue, useAtom, useCreateAtom, useCreateStore
    • createStoreContext for typed store context support
    • Added many React examples showcasing the new hooks and patterns
  • Documentation

    • Full reference pages and quick-start updates for the new hooks and options
  • Deprecations

    • useStore marked deprecated in favor of useSelector
  • Chores

    • Standardized local example dev server port to 3050 and updated example manifests

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

📝 Walkthrough

Walkthrough

Adds a set of React hooks and context utilities for TanStack Store (useSelector, useValue, useSetValue, useAtom, useCreateAtom, useCreateStore, createStoreContext), extracts/refactors shallow and useStore (deprecated alias), updates docs, examples, package exports, and expands tests.

Changes

Cohort / File(s) Summary
Changeset Entry
.changeset/petite-actors-lie.md
New changeset marking minor version bumps and build/process notes.
React API docs & nav
docs/config.json, docs/framework/react/reference/index.md, docs/framework/react/reference/functions/*.md, docs/framework/react/reference/interfaces/UseSelectorOptions.md, docs/framework/react/quick-start.md
Adds docs for new hooks/interfaces, updates navigation to replace/useSelector for useStore, marks useStore deprecated, and updates quick-start examples.
React examples (new)
examples/react/atoms/..., examples/react/stores/..., examples/react/store-context/...
Adds three React example projects demonstrating atom hooks, store hooks, and store-context usage (entrypoints, package.json, tsconfigs, HTML).
React examples (updated)
examples/react/simple/*, examples/*/simple/package.json, examples/*/simple/angular.json
Standardizes dev ports to 3050, replaces useStore with useSelector in simple React example, removes react-scan where applicable, and adjusts framework example configs.
React runtime - new hooks & context
packages/react-store/src/useSelector.ts, useValue.ts, useSetValue.ts, useAtom.ts, useCreateAtom.ts, useCreateStore.ts, createRequiredContext.ts, createStoreContext.ts
Implements new hooks and a typed store-context factory: selection, whole-value read, stable setters, combined read/write atom hook, and mount-stable atom/store creation; adds required-context helper.
Refactor exports & utilities
packages/react-store/src/index.ts, packages/react-store/src/useStore.ts, packages/react-store/src/shallow.ts
Reworks barrel exports to expose new hooks, moves shallow into its own module, and turns useStore into a deprecated wrapper forwarding to useSelector.
Tests & types
packages/react-store/tests/index.test.tsx, packages/react-store/tests/test.test-d.ts
Substantially expands runtime and type tests to cover the new hooks, context behavior, readonly vs writable variants, and useStore compatibility.
Package metadata / build
package.json, packages/*-store/package.json (react, vue, preact, solid, angular, store, etc.)
Updates dev dependency versions, changes published types paths to .d.cts, and simplifies package exports mappings (remove conditional types entries).

Sequence Diagram(s)

sequenceDiagram
    participant App as React App
    participant Provider as StoreProvider
    participant Context as StoreContext
    participant Component as Component
    participant Source as Atom/Store

    App->>Provider: create store/atom via useCreateStore/useCreateAtom
    App->>Provider: wrap children with StoreProvider(value)
    Provider->>Context: provide keyed value ({ store, atom })
    Component->>Context: useStoreContext()
    Context-->>Component: returns { store, atom }
    Component->>Source: useSelector(source, selector) / useValue(source)
    Source-->>Component: selected or full value
    Component->>Source: useSetValue(source) / useAtom(source)
    Source-->>Component: returns stable setter / [value, setter]
    Component->>Source: invoke setter -> update
    Source->>Component: subscription triggers re-render with new value
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • crutchcorn

Poem

🐰 Hopped in code with eager paws,

I stitched new hooks and trimmed old laws,
Context baskets, selectors bright,
Setters steady, types just right,
I nibble bugs and celebrate the PR’s applause!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.28% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: new react hooks' accurately summarizes the main change: introduction of new React hooks and related utilities for the @tanstack/react-store package.
Description check ✅ Passed The pull request description is complete and well-structured, covering all new hooks, their purposes, and checklist items. A changeset entry was properly generated.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch hooks

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 11, 2026

View your CI Pipeline Execution ↗ for commit 47f9806

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 41s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 9s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-11 18:20:54 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 11, 2026

🚀 Changeset Version Preview

7 package(s) bumped directly, 0 bumped as dependents.

🟨 Minor bumps

Package Version Reason
@tanstack/angular-store 0.10.0 → 0.11.0 Changeset
@tanstack/preact-store 0.12.0 → 0.13.0 Changeset
@tanstack/react-store 0.10.0 → 0.11.0 Changeset
@tanstack/solid-store 0.10.0 → 0.11.0 Changeset
@tanstack/store 0.10.0 → 0.11.0 Changeset
@tanstack/svelte-store 0.11.0 → 0.12.0 Changeset
@tanstack/vue-store 0.10.0 → 0.11.0 Changeset

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 11, 2026

@tanstack/angular-store

npm i https://pkg.pr.new/@tanstack/angular-store@306

@tanstack/preact-store

npm i https://pkg.pr.new/@tanstack/preact-store@306

@tanstack/react-store

npm i https://pkg.pr.new/@tanstack/react-store@306

@tanstack/solid-store

npm i https://pkg.pr.new/@tanstack/solid-store@306

@tanstack/store

npm i https://pkg.pr.new/@tanstack/store@306

@tanstack/svelte-store

npm i https://pkg.pr.new/@tanstack/svelte-store@306

@tanstack/vue-store

npm i https://pkg.pr.new/@tanstack/vue-store@306

commit: 47f9806

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 10

🧹 Nitpick comments (1)
packages/react-store/src/createRequiredContext.ts (1)

4-25: Consider making context name configurable for reusable diagnostics.

The helper is generic, but the current error/display name are store-specific. Parameterizing the name would keep future usage messages accurate.

Possible refactor
-export function createRequiredContext<TValue>() {
+export function createRequiredContext<TValue>(contextName = 'StoreContext') {
   const Context = createContext<TValue | null>(null)
-  Context.displayName = 'StoreContext'
+  Context.displayName = contextName
@@
     if (value === null) {
-      throw new Error('Missing StoreProvider for StoreContext')
+      throw new Error(`Missing Provider for ${contextName}`)
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-store/src/createRequiredContext.ts` around lines 4 - 25,
Change createRequiredContext to accept an optional context name parameter (e.g.,
createRequiredContext<TValue>(contextName = 'StoreContext')) and use that
parameter to set Context.displayName and to build the thrown error in
useRequiredValue; update references to Context.displayName and the error message
string ("Missing StoreProvider for StoreContext") to interpolate the provided
contextName so diagnostics reflect the caller-provided name, and keep Provider
and useRequiredValue behavior the same.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.changeset/petite-actors-lie.md:
- Around line 2-11: The changeset .changeset/petite-actors-lie.md currently
lists blanket minor bumps for many framework packages and a generic message
("chore: update deps and change build process to tsdown") that doesn't match the
PR's "new React hooks" feature; update the changeset to either (a) limit the
package entries to only the affected package(s) (e.g., '@tanstack/react-store')
and change the message to describe the new React hooks, or (b) split into two
changesets—one describing the new React hooks for the React package and a
separate one for dependency/build changes—so release notes and version bumps
accurately reflect the feature scope.

In `@docs/framework/react/reference/functions/createStoreContext.md`:
- Around line 9-33: Docs currently show createStoreContext<TValue>(): object but
the implementation requires TValue extends object and returns a typed store
context provider object; update the doc signature to declare the generic
constraint (TValue extends object) and replace the vague return type "object"
with the precise typed context return (reference the createStoreContext function
and the StoreProvider it returns, and mention
useStoreContext/useSelector/useValue hooks so readers see the linkage) so the
docs match the implementation's API contract.

In `@docs/framework/react/reference/functions/useCreateAtom.md`:
- Line 48: The markdown contains a duplicated heading "## Call Signature" for
useCreateAtom which triggers MD024; edit the second occurrence so it is unique
(e.g., change it to "## Call Signature (overload 2)" or "## Call Signature —
Overload 2") to avoid duplicate-heading lint warnings while preserving the
overload content for the useCreateAtom documentation.

In `@docs/framework/react/reference/functions/useCreateStore.md`:
- Around line 8-44: The docs have duplicate "## Call Signature" headings which
triggers MD024; edit the useCreateStore documentation so each overload section
has a unique heading (e.g., rename one to "## Call Signatures" or "## Call
Signature (overload)" or similar), ensuring the references to the function
useCreateStore and the content under the original first block remain unchanged
and only the trailing duplicate heading is renamed or removed to eliminate the
duplicate heading conflict.

In `@docs/framework/react/reference/functions/useSetValue.md`:
- Line 50: The docs file has duplicate "## Call Signature" headings for the
useSetValue function which can trigger markdownlint MD024; update the second
overload heading to a unique label (e.g., "## Call Signature (overload 2)" or
"## Call Signature — overloaded") so the overload sections for useSetValue are
distinct and avoid the lint rule.

In `@examples/react/atoms/index.html`:
- Line 10: Replace the unversioned CDN script URL in the HTML (the <script
src="https://unpkg.com/react-scan/dist/auto.global.js"> tag) with a pinned
package version and add Subresource Integrity and crossorigin attributes; e.g.,
change the src to include a specific version like react-scan@0.3.4, fetch the
integrity value from https://unpkg.com/react-scan@0.3.4/dist/auto.global.js?meta
and add integrity="..." and crossorigin="anonymous" to the script tag to prevent
tampering and unexpected updates.

In `@examples/react/atoms/src/index.tsx`:
- Around line 2-8: The import mixes a type with value imports which violates
import/consistent-type-specifier-style; separate the type import from runtime
imports by creating a dedicated type-only import for Atom (e.g., import type {
Atom } from '@tanstack/react-store') and keep useAtom, useCreateAtom,
useSetValue, useValue in a separate non-type import from '@tanstack/react-store'
so the type and value imports are in distinct statements.

In `@examples/react/simple/index.html`:
- Line 14: Update the floating CDN import script tag (the line with
src="https://unpkg.com/react-scan/dist/auto.global.js") to pin to
react-scan@0.5.3 by changing the src to
https://unpkg.com/react-scan@0.5.3/dist/auto.global.js and add an integrity
attribute containing the correct SRI hash for that exact file plus
crossorigin="anonymous"; obtain the precise SRI (sha256/sha384/sha512) from the
published artifact or a trusted SRI tool and paste it into the integrity
attribute so the script tag includes both integrity="..." and
crossorigin="anonymous".

In `@examples/react/store-context/src/index.tsx`:
- Around line 2-10: The import list from '@tanstack/react-store' is not
alphabetized (violates sort-imports); reorder the named imports so
createStoreContext appears first and the rest follow alphabetically (e.g.,
createStoreContext, useAtom, useCreateAtom, useCreateStore, useSelector,
useSetValue, useValue) to satisfy the linter rule and keep the import statement
consistent.

In `@examples/react/stores/src/index.tsx`:
- Line 2: Change the mixed import to use a top-level type-only import for the
Store type: import type { Store } separately and keep the runtime imports
(useCreateStore, useSelector) as normal imports; update the import statement(s)
in index.tsx so Store is imported with the `import type` form to satisfy
import/consistent-type-specifier-style while leaving useCreateStore and
useSelector as runtime imports.

---

Nitpick comments:
In `@packages/react-store/src/createRequiredContext.ts`:
- Around line 4-25: Change createRequiredContext to accept an optional context
name parameter (e.g., createRequiredContext<TValue>(contextName =
'StoreContext')) and use that parameter to set Context.displayName and to build
the thrown error in useRequiredValue; update references to Context.displayName
and the error message string ("Missing StoreProvider for StoreContext") to
interpolate the provided contextName so diagnostics reflect the caller-provided
name, and keep Provider and useRequiredValue behavior the same.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 23a04a85-8c1a-486d-a0d5-e429944d1e62

📥 Commits

Reviewing files that changed from the base of the PR and between 840b9d0 and 154e0a8.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (52)
  • .changeset/petite-actors-lie.md
  • docs/config.json
  • docs/framework/react/quick-start.md
  • docs/framework/react/reference/functions/createStoreContext.md
  • docs/framework/react/reference/functions/shallow.md
  • docs/framework/react/reference/functions/useAtom.md
  • docs/framework/react/reference/functions/useCreateAtom.md
  • docs/framework/react/reference/functions/useCreateStore.md
  • docs/framework/react/reference/functions/useSelector.md
  • docs/framework/react/reference/functions/useSetValue.md
  • docs/framework/react/reference/functions/useStore.md
  • docs/framework/react/reference/functions/useValue.md
  • docs/framework/react/reference/index.md
  • docs/framework/react/reference/interfaces/UseSelectorOptions.md
  • examples/angular/simple/angular.json
  • examples/preact/simple/package.json
  • examples/react/atoms/README.md
  • examples/react/atoms/index.html
  • examples/react/atoms/package.json
  • examples/react/atoms/src/index.tsx
  • examples/react/atoms/tsconfig.json
  • examples/react/simple/index.html
  • examples/react/simple/package.json
  • examples/react/simple/src/index.tsx
  • examples/react/store-context/README.md
  • examples/react/store-context/index.html
  • examples/react/store-context/package.json
  • examples/react/store-context/src/index.tsx
  • examples/react/store-context/src/vite-env.d.ts
  • examples/react/store-context/tsconfig.json
  • examples/react/store-context/vite.config.ts
  • examples/react/stores/README.md
  • examples/react/stores/index.html
  • examples/react/stores/package.json
  • examples/react/stores/src/index.tsx
  • examples/react/stores/tsconfig.json
  • examples/solid/simple/package.json
  • examples/svelte/simple/package.json
  • examples/vue/simple/package.json
  • packages/react-store/src/createRequiredContext.ts
  • packages/react-store/src/createStoreContext.ts
  • packages/react-store/src/index.ts
  • packages/react-store/src/shallow.ts
  • packages/react-store/src/useAtom.ts
  • packages/react-store/src/useCreateAtom.ts
  • packages/react-store/src/useCreateStore.ts
  • packages/react-store/src/useSelector.ts
  • packages/react-store/src/useSetValue.ts
  • packages/react-store/src/useStore.ts
  • packages/react-store/src/useValue.ts
  • packages/react-store/tests/index.test.tsx
  • packages/react-store/tests/test.test-d.ts

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