feat: Support isolated nested state via StateScopeProvider#21
feat: Support isolated nested state via StateScopeProvider#21vothanhdat merged 10 commits intomasterfrom
Conversation
- 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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
- 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>
There was a problem hiding this comment.
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
StateScopeProviderto create an isolated scope and mount a scope-localAutoRootCtx. - Makes
useDataContextscope-aware by prefixing context names with the active scope ID. - Updates
createRootCtxduplicate-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. |
| return () => clearTimeout(timeout) | ||
| } | ||
| }, [ctxMountedCheck.has(ctxName)]) | ||
| }, [ctxMountedCheck.has(scopedCtxName)]) | ||
|
|
||
| return useDataContext<V>(ctxName) |
There was a problem hiding this comment.
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.
| import { AutoRootCtx, StateScopeProvider } from 'react-state-custom' | ||
|
|
There was a problem hiding this comment.
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).
| 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}</> | |
| } |
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>
|
@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. |
Co-authored-by: vothanhdat <8221386+vothanhdat@users.noreply.github.com>
[WIP] Update to address re-exporting feedback in StateScopeProvider PR
This PR introduces the
StateScopeProvidercomponent, allowing developers to create isolated state scopes within their React application.Key Changes:
StateScopeProvider: A new component that generates a unique scope ID (viauseId) and provides it to its children viaStateScopeContext. It also renders an internalAutoRootCtxto manage the roots for that specific scope.useDataContext: TheuseDataContexthook now reads the currentStateScopeContext. If a scope ID is present, it prefixes the context name (e.g.,":r1:/my-store"), ensuring data isolation.createRootCtxfactory now includes the scope ID when checking for duplicate root mounts, preventing false positives when the same store is used in different scopes.Usage:
Testing:
tests/createAutoCtx.nested.test.tsxwhich confirms that state updates in an outer scope do not affect the inner scope and vice-versa.PR created automatically by Jules for task 12691107722258462697 started by @quynhtrang0309