pmndrs / jotai

👻 Primitive and flexible state management for React
https://jotai.org
MIT License
18.34k stars 589 forks source link

idea3: atomFamily supports instanceof #2681

Closed dmaskasky closed 1 month ago

dmaskasky commented 1 month ago

Related Bug Reports or Discussions

idea1: https://github.com/pmndrs/jotai/pull/2678 idea2: https://github.com/pmndrs/jotai/pull/2679

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

Summary

jotai-scope is trying to support scoping atomFamily.

const fooFamily = atomFamily((id) => atom(id))
<ScopedProvider atomFamilies={[fooFamily]}>{children}</ScopeProvider>

This PR demonstrates one approach in which the atomFamily supports use of instanceof language feature.

The following pseudo code represents logic that would live in jotai-scope.

const isExplicitlyScoped = atomSet.has(atom) || Array.from(atomFamilySet).some(af => atom instanceof af)

Check List

vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 30, 2024 2:02pm
codesandbox-ci[bot] commented 1 month 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.

github-actions[bot] commented 1 month ago

LiveCodes Preview in LiveCodes

Latest commit: cb641c86cf233541d59740085a40d0b7fd59d550
Last updated: Jul 30, 2024 2:02pm (UTC)

Playground Link
React demo https://livecodes.io?x=id/DTWHNYL4W

See documentations for usage instructions.

dai-shi commented 1 month ago

I don't think this complexity and the trick should be added in this util. Please consider patching atomFamily in jotai-scope.

dai-shi commented 1 month ago

If we were to go this road, I think I'd prefer adding .has(atom) method. But it feels too specific to jotai-scope.