@@ -102,66 +102,136 @@ Available options:
102102}
103103```
104104
105+ ### Store API
106+
107+ The ` createAtomStore ` function returns an object with the following:
108+
109+ ``` ts
110+ const {
111+ // Store name used as prefix
112+ name : string,
113+
114+ // Store hook returning all utilities
115+ useAppStore : () = > StoreApi ,
116+
117+ // Direct hooks for state management
118+ useAppValue : (key : string, options?) = > Value ,
119+ useAppSet : (key : string) = > SetterFn ,
120+ useAppState : (key : string) = > [Value , SetterFn ],
121+
122+ // Provider component
123+ AppProvider : React.FC<ProviderProps>,
124+
125+ // Record of all atoms in the store
126+ appStore : {
127+ atom : Record<string, Atom>
128+ }
129+ } = createAtomStore ({ ... }, { name: ' app' });
130+ ```
131+
105132### Reading and Writing State
106133
107- The API is designed to be intuitive. Here's how you work with state:
134+ There are three ways to interact with the store state:
135+
136+ #### 1. Hooks (Recommended)
108137
109- #### Using Hooks (Recommended)
138+ The most straightforward way using hooks returned by ` createAtomStore ` :
110139
111140``` ts
112- // Get a single value
113- const name = useUserValue (' name' );
114- const loggedIn = useUserValue ( ' loggedIn ' );
141+ // Get value
142+ const name = useAppValue (' name' );
143+ const stars = useAppValue ( ' stars ' );
115144
116- // Get a setter
117- const setName = useUserSet (' name' );
118- const setLoggedIn = useUserSet ( ' loggedIn ' );
145+ // Set value
146+ const setName = useAppSet (' name' );
147+ const setStars = useAppSet ( ' stars ' );
119148
120149// Get both value and setter
121- const [name, setName] = useUserState (' name' );
122- const [loggedIn, setLoggedIn] = useUserState (' loggedIn' );
123-
124- // With selector
125- const upperName = useUserValue (' name' , (name ) => name .toUpperCase ());
150+ const [name, setName] = useAppState (' name' );
151+ const [stars, setStars] = useAppState (' stars' );
126152
127153// With selector and deps
128- const nthChar = useUserValue (' name' , (name ) => name [n ], [n ]);
154+ const upperName = useAppValue (' name' , {
155+ selector : (name ) => name .toUpperCase (),
156+ deps: []
157+ });
158+ ```
159+
160+ #### 2. Store Instance Methods
161+
162+ Using the store instance from ` useAppStore() ` :
163+
164+ ``` ts
165+ const store = useAppStore ();
166+
167+ // By key
168+ store .get (' name' ); // Get value
169+ store .set (' name' , ' value' ); // Set value
170+ store .subscribe (' name' , (value ) => console .log (value )); // Subscribe to changes
171+
172+ // Direct access
173+ store .getName (); // Get value
174+ store .setName (' value' ); // Set value
175+ store .subscribeName ((value ) => console .log (value )); // Subscribe to changes
129176```
130177
131- ### React Hooks
178+ #### 3. Raw Atom Access
179+
180+ For advanced use cases, you can work directly with atoms:
181+
182+ ``` ts
183+ const store = useAppStore ();
184+
185+ // Access atoms
186+ store .getAtom (someAtom ); // Get atom value
187+ store .setAtom (someAtom , ' value' ); // Set atom value
188+ store .subscribeAtom (someAtom , (value ) => {}); // Subscribe to atom
132189
133- #### ` useStoreValue(key, selector?, deps?) `
190+ // Access underlying Jotai store
191+ const jotaiStore = store .store ;
192+ ```
134193
135- Subscribe to a single value. Optionally pass a selector and deps array:
194+ ### Hook API Reference
195+
196+ #### ` use<Name>Value(key, options?) `
197+
198+ Subscribe to a single value with optional selector and deps:
136199
137200``` ts
138201// Basic usage
139- const name = useUserValue (' name' );
202+ const name = useAppValue (' name' );
140203
141204// With selector
142- const upperName = useUserValue (' name' , (name ) => name .toUpperCase ());
143-
144- // With selector and deps
145- const nthChar = useUserValue (' name' , (name ) => name [n ], [n ]);
205+ const upperName = useAppValue (' name' , {
206+ selector : (name ) => name .toUpperCase (),
207+ deps: [] // Optional deps array
208+ });
209+
210+ // With equality function
211+ const name = useAppValue (' name' , {
212+ selector : (name ) => name ,
213+ equalityFn : (prev , next ) => prev .length === next .length
214+ });
146215```
147216
148- #### ` useStoreSet (key)`
217+ #### ` use<Name>Set (key)`
149218
150219Get a setter function for a value:
151220
152221``` ts
153- // Basic usage
154- const setName = useUserSet (' name' );
222+ const setName = useAppSet (' name' );
223+ setName (' new value' );
224+ setName ((prev ) => prev .toUpperCase ());
155225```
156226
157- #### ` useStoreState (key)`
227+ #### ` use<Name>State (key)`
158228
159- Get a value and its setter, just like React's ` useState ` :
229+ Get both value and setter, like React's ` useState ` :
160230
161231``` tsx
162232function UserForm() {
163- const [name, setName] = useUserState (' name' );
164- const [email, setEmail] = useUserState (' email' );
233+ const [name, setName] = useAppState (' name' );
234+ const [email, setEmail] = useAppState (' email' );
165235
166236 return (
167237 <form >
@@ -174,18 +244,45 @@ function UserForm() {
174244
175245### Provider-Based Store Hydration
176246
177- The provider component handles hydrating and syncing the store's state:
247+ The provider component handles store initialization and state synchronization :
178248
179249``` tsx
250+ type ProviderProps <T > = {
251+ // Initial values for atoms, hydrated once on mount
252+ initialValues? : Partial <T >;
253+
254+ // Dynamic values for controlled state
255+ ... Partial <T >;
256+
257+ // Optional custom store instance
258+ store? : JotaiStore ;
259+
260+ // Optional scope for nested providers
261+ scope? : string ;
262+
263+ // Optional key to reset the store
264+ resetKey? : any ;
265+
266+ children: React .ReactNode ;
267+ };
268+
180269function App() {
181270 return (
182271 <UserProvider
272+ // Initial values hydrated on mount
183273 initialValues = { {
184274 name: ' Alice' ,
185- onUpdateName : ( name ) => console . log ( name ),
275+ email: ' alice@example.com '
186276 }}
187- // Alternative to initialValues
188- name = " Alice"
277+
278+ // Controlled values that sync with the store
279+ name = " Bob"
280+
281+ // Optional scope for nested providers
282+ scope = " user1"
283+
284+ // Optional key to reset store state
285+ resetKey = { version }
189286 >
190287 <UserProfile />
191288 </UserProvider >
0 commit comments