jotaijs / jotai-scope

MIT License
60 stars 4 forks source link

Feat: Add Support for Scoping `atomFamily` in Jotai #51

Closed dmaskasky closed 2 months ago

dmaskasky commented 2 months ago

Summary

Adds support for scoping atomFamily instances within the ScopeProvider. This allows for better state isolation and management when using atomFamily, enabling scoped states to be cleanly separated and managed within specific contexts.

const aFamily = atomFamily(() => atom(0));

<ScopeProvider atomFamilies={[aFamily]}>
  {children}
</ScopeProvider>

Motivation

The ability to scope atomFamilies was identified as a necessary feature for more complex state management scenarios, particularly in larger applications where isolated state is crucial for maintainability and performance.

Fixes https://github.com/jotaijs/jotai-scope/issues/50

Future Work

The tests for atom removal in scoped atomFamilies are currently marked as TODO. Refactoring atomFamily may be required to support descoping and removing atoms.

Impact

This update will primarily benefit developers who need to manage complex state scenarios involving atomFamily. Existing functionality remains unaffected, but users will need to ensure compatibility with the updated Jotai version.

codesandbox-ci[bot] commented 2 months ago

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.

scornz commented 1 month ago

Quick question on the implementation: once the ScopeProvider unmounts, should all the keys that were set within that scope be automatically removed?

dmaskasky commented 1 month ago

Quick question on the implementation: once the ScopeProvider unmounts, should all the keys that were set within that scope be automatically removed?

Yes. When the ScopeProvider unmounts, atomSet and atomFamilySet are no longer referenced and are available for garbage collection.