Gathering detailed insights and metrics for jotai-sync-scope
Gathering detailed insights and metrics for jotai-sync-scope
Gathering detailed insights and metrics for jotai-sync-scope
Gathering detailed insights and metrics for jotai-sync-scope
Library that provides a way to sync Jotai atoms within some scope
npm install jotai-sync-scope
Typescript
Module System
Node Version
NPM Version
66
Supply Chain
90.5
Quality
74.8
Maintenance
100
Vulnerability
100
License
TypeScript (97.71%)
HTML (2.05%)
JavaScript (0.24%)
Total Downloads
133
Last Day
1
Last Week
1
Last Month
4
Last Year
48
MIT License
2 Stars
22 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Feb 11, 2025
Latest Version
0.0.1
Package Id
jotai-sync-scope@0.0.1
Unpacked Size
11.74 kB
Size
3.35 kB
File Count
8
NPM Version
10.5.0
Node Version
18.20.2
Published on
May 12, 2024
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
1
Compared to previous week
Last Month
100%
4
Compared to previous month
Last Year
-43.5%
48
Compared to previous year
The idea is to sync two Jotai atoms by basically making writing to synced atom to write to original atom.
Backwards should work too. Writing to source atom should change value in target atom
1import { atom, useAtom } from 'jotai'; 2import { SyncScopeProvider } from 'jotai-sync-scope'; 3 4const sourceAtom = atom(0); 5const targetAtom = atom(0); 6 7const App = () => { 8 const [state] = useAtom(sourceAtom); 9 10 return ( 11 <> 12 <p>Source: {state}</p> 13 <Sync /> 14 </> 15 ); 16}; 17 18const Sync = (props) => { 19 return ( 20 <SyncScopeProvider atoms={[[sourceAtom, targetAtom]]}> 21 <Component /> 22 </SyncScopeProvider> 23 ); 24}; 25 26const Component = () => { 27 const [state, setState] = useAtom(targetAtom); 28 29 return ( 30 <> 31 <p>Target: {state}</p> 32 <button onClick={() => setState((s) => s + 1)}>+1</button> 33 </> 34 ); 35};
One of the use cases is when you have atom with array of items and you want to provide a way to consume item within scope of some components using splitAtom
and hook to the item like it's global.
1import { atom, useAtom, useAtomValue, atom } from 'jotai'; 2import { splitAtom } from 'jotai/utils'; 3import { SyncScopeProvider } from 'jotai-sync-scope'; 4 5const tabsAtom = atom([]); 6const tabAtomsAtom = splitAtom(tabsAtom); 7 8const App = () => { 9 const tabAtoms = useAtomValue(tabAtomsAtom); 10 11 return ( 12 <> 13 {tabAtoms.map((atom) => ( 14 <SyncScopeProvider atoms={[[atom, tabAtom]]} key={atom.key}> 15 <Tab /> 16 </SyncScopeProvider> 17 ))} 18 </> 19 ); 20}; 21 22const tabAtom = atom({}); 23 24const Tab = () => { 25 const [tab, setTab] = useAtom(tabAtom); 26 27 // You can update tabAtom value as if it's global but it's actually relates to an item from tabsAtom. 28}; 29 30// You can even derive some state from tabAtom and use it to access some part of it 31const tabName = atom((get) => get(tabAtom).title);
No vulnerabilities found.