Skip to content

feat: Support isolated nested state via StateScopeProvider#21

Merged
vothanhdat merged 10 commits intomasterfrom
feature/isolated-nested-state-12691107722258462697
Feb 22, 2026
Merged

feat: Support isolated nested state via StateScopeProvider#21
vothanhdat merged 10 commits intomasterfrom
feature/isolated-nested-state-12691107722258462697

Conversation

@quynhtrang0309
Copy link
Copy Markdown
Collaborator

This PR introduces the StateScopeProvider component, allowing developers to create isolated state scopes within their React application.

Key Changes:

  1. StateScopeProvider: A new component that generates a unique scope ID (via useId) and provides it to its children via StateScopeContext. It also renders an internal AutoRootCtx to manage the roots for that specific scope.
  2. Scope-Aware useDataContext: The useDataContext hook now reads the current StateScopeContext. If a scope ID is present, it prefixes the context name (e.g., ":r1:/my-store"), ensuring data isolation.
  3. Duplicate Mount Check: The createRootCtx factory now includes the scope ID when checking for duplicate root mounts, preventing false positives when the same store is used in different scopes.

Usage:

<AutoRootCtx>
  {/* Global Scope */}
  <App />
  
  <StateScopeProvider>
     {/* Isolated Scope - Stores here are independent of Global Scope */}
     <Feature />
  </StateScopeProvider>
</AutoRootCtx>

Testing:

  • Added tests/createAutoCtx.nested.test.tsx which confirms that state updates in an outer scope do not affect the inner scope and vice-versa.
  • Validated that existing global state behavior remains unchanged.

PR created automatically by Jules for task 12691107722258462697 started by @quynhtrang0309

- Introduced `StateScopeContext` and `StateScopeProvider` to manage isolated state scopes.
- `StateScopeProvider` uses `React.useId` to generate a unique scope ID and renders an internal `AutoRootCtx` manager.
- Updated `useDataContext` to automatically namespace context names with the current scope ID.
- Updated `createRootCtx` to respect scope when checking for duplicate root mounts.
- Added regression test `tests/createAutoCtx.nested.test.tsx` verifying isolation between nested providers.

Co-authored-by: quynhtrang0309 <247292619+quynhtrang0309@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules bot and others added 2 commits February 21, 2026 03:54
- Introduced `StateScopeContext` and `StateScopeProvider` to manage isolated state scopes.
- `StateScopeProvider` uses `React.useId` to generate a unique scope ID and renders an internal `AutoRootCtx` manager.
- Updated `useDataContext` to automatically namespace context names with the current scope ID.
- Updated `createRootCtx` to respect scope when checking for duplicate root mounts.
- Added regression test `tests/createAutoCtx.nested.test.tsx` verifying isolation between nested providers.

Co-authored-by: quynhtrang0309 <247292619+quynhtrang0309@users.noreply.github.com>
- Introduced `StateScopeContext` and `StateScopeProvider` to manage isolated state scopes.
- `StateScopeProvider` uses `React.useId` to generate a unique scope ID and renders an internal `AutoRootCtx` manager.
- Updated `useDataContext` to automatically namespace context names with the current scope ID.
- Updated `createRootCtx` to respect scope when checking for duplicate root mounts.
- Added regression test `tests/createAutoCtx.nested.test.tsx` verifying isolation between nested providers.
- Updated README.md and API_DOCUMENTATION.md with instructions for Isolated State.

Co-authored-by: quynhtrang0309 <247292619+quynhtrang0309@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

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

Adds first-class “state scoping” to react-state-custom so nested subtrees can run isolated instances of the same stores without cross-talk, while keeping existing global behavior unchanged when no scope is present.

Changes:

  • Introduces StateScopeProvider to create an isolated scope and mount a scope-local AutoRootCtx.
  • Makes useDataContext scope-aware by prefixing context names with the active scope ID.
  • Updates createRootCtx duplicate-mount checks to include scope in the key; adds a nested-scope isolation test and updates docs.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/createAutoCtx.nested.test.tsx Adds a regression test validating outer/inner scope isolation.
src/state-utils/ctx.ts Adds StateScopeContext and scope-aware context name resolution in useDataContext.
src/state-utils/createRootCtx.tsx Includes scope in duplicate mount / missing-root checks.
src/state-utils/createAutoCtx.tsx Adds StateScopeProvider (scope ID + scoped AutoRootCtx).
README.md Documents isolated state usage via StateScopeProvider.
API_DOCUMENTATION.md Adds API reference section for StateScopeProvider.

Comment on lines 136 to 140
return () => clearTimeout(timeout)
}
}, [ctxMountedCheck.has(ctxName)])
}, [ctxMountedCheck.has(scopedCtxName)])

return useDataContext<V>(ctxName)
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

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

useCtxState’s mount-check effect depends on ctxMountedCheck.has(scopedCtxName), which won’t re-run/cleanup when the Root becomes mounted because the Set mutation doesn’t trigger a re-render. This can cause a delayed console.error even when the Root mounts successfully. Use a stable dependency like scopedCtxName (and re-check inside the effect) so the timeout can be cleared when appropriate.

Copilot uses AI. Check for mistakes.
Comment on lines +124 to +125
import { AutoRootCtx, StateScopeProvider } from 'react-state-custom'

Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

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

The README example imports StateScopeProvider from react-state-custom, but it is not currently re-exported from the package entrypoint (src/index.ts). Consumers following this snippet will get a missing export error; please re-export StateScopeProvider from the public index (or adjust the docs if it’s intentionally internal).

Suggested change
import { AutoRootCtx, StateScopeProvider } from 'react-state-custom'
import { AutoRootCtx } from 'react-state-custom'
// Example of a custom scope provider component wrapping an isolated part of your app.
function StateScopeProvider({ children }: { children: React.ReactNode }) {
return <>{children}</>
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot re-export it

vothanhdat and others added 5 commits February 21, 2026 18:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 21, 2026

@vothanhdat I've opened a new pull request, #22, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits February 21, 2026 11:31
Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
[WIP] Update to address re-exporting feedback in StateScopeProvider PR
Copy link
Copy Markdown
Contributor

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 6 out of 6 changed files in this pull request and generated no new comments.

@vothanhdat vothanhdat merged commit aeb078d into master Feb 22, 2026
7 checks passed
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.

4 participants