@@ -23,52 +23,27 @@ In this basic guide we’ll cover:
2323
2424Here we describe state and actions using an Typescript interface. The ` <BearState> `  generic forces the store to match this shape.
2525This means if you forget a field or use the wrong type, TypeScript will complain. Unlike plain JS, this guarantees type-safe state management.
26+ The ` create `  function uses the curried form, which results in a store of type ` UseBoundStore<StoreApi<BearState>> ` .
2627
2728``` ts 
29+ //  store.ts
2830import  { create  } from  ' zustand' 
2931
3032//  Define types for state & actions
3133interface  BearState  {
3234  bears:  number 
3335  food:  string 
34-   increase:  (by :  number ) =>  void 
35-   feed:  (food :  string ) =>  void 
36- }
37- 
38- //  Create store with explicit generic <BearState>
39- export const =  create <BearState >((set ) =>  ({
40-   bears: 2 ,
41-   food: ' honey' 
42-   increase : (by ) =>  set ((state ) =>  ({ bears: state .bears  +  by  })),
43-   feed : (food ) =>  set (() =>  ({ food  })),
44- }))
45- ``` 
46- 
47- ` create `  also supports a curried version. It's useful when you want to fix the state type first and initialize later (for example, inside a factory).
48- 
49- ``` ts 
50- import  { create  } from  ' zustand' 
51- 
52- //  Define types for state & actions
53- interface  BearState  {
54-   bears:  number 
55-   food:  string 
56-   increase:  (by :  number ) =>  void 
5736  feed:  (food :  string ) =>  void 
5837}
5938
60- //  Create store using the curried form of the  `create`
39+ //  Create store using the curried form of `create`
6140export const =  create <BearState >()((set ) =>  ({
6241  bears: 2 ,
6342  food: ' honey' 
64-   increase : (by ) =>  set ((state ) =>  ({ bears: state .bears  +  by  })),
6543  feed : (food ) =>  set (() =>  ({ food  })),
6644}))
6745``` 
6846
69- Both forms are functionally identical. They produce the same store type ` UseBoundStore<StoreApi<BearState>> ` .
70- The curried form is just syntactic sugar for scenarios where you want more flexibility in how and when you define your initializer.
71- 
7247### Using the Store in Components  
7348
7449Inside components, you can read state and call actions. Selectors ` (s) => s.bears `  subscribe to only what you need.
@@ -88,9 +63,9 @@ function BearFeeder() {
8863  const =  useBearStore ()
8964  return  (
9065    < button  onClick = {() => feed(" berries" > 
91- 		 Feed  bears  {food }
92- 		 < / button > 
93- 	 )
66+        Feed  bears  {food }
67+      < / button > 
68+    )
9469}
9570``` 
9671
@@ -108,7 +83,7 @@ interface BearState {
10883  addBear:  () =>  void 
10984}
11085
111- export   const =  create <BearState >((set ) =>  ({
86+ const =  create <BearState >() ((set ) =>  ({
11287  bears: 2 ,
11388  addBear : () =>  set ((s ) =>  ({ bears: s .bears  +  1  })),
11489}))
@@ -119,17 +94,27 @@ interface FishState {
11994  addFish:  () =>  void 
12095}
12196
122- export   const =  create <FishState >((set ) =>  ({
97+ const =  create <FishState >() ((set ) =>  ({
12398  fish: 5 ,
12499  addFish : () =>  set ((s ) =>  ({ fish: s .fish  +  1  })),
125100}))
126101
127- //  In components you can use both stores safely
102+ //  In components,  you can use both stores safely
128103function  Zoo() {
129-   const   =   useBearStore (( s )  =>   s . bears )
130-   const   =   useFishStore (( s )  =>   s . fish )
104+   const {  bears, addBear }  =   useBearStore ( )
105+   const {  fish, addFish }  =   useFishStore ( )
131106
132-   return  <div >{bears } bears  and  {fish } fish < / div > 
107+   return  (
108+     <div >
109+       <div >{bears } bears  and  {fish } fish < / div > 
110+       < button  onClick = {addBear }> 
111+         Add  bear 
112+       < / button > 
113+       < button  onClick = {addFish }> 
114+         Add  fish 
115+       < / button > 
116+     < / div > 
117+   )
133118}
134119``` 
135120
@@ -149,11 +134,27 @@ type BearState = typeof initialState & {
149134  reset:  () =>  void 
150135}
151136
152- export   const =  create <BearState >((set ) =>  ({
137+ const =  create <BearState >() ((set ) =>  ({
153138  ... initialState ,
154139  increase : (by ) =>  set ((s ) =>  ({ bears: s .bears  +  by  })),
155140  reset : () =>  set (initialState ),
156141}))
142+ 
143+ function  ResetZoo() {
144+   const =  useBearStore ()
145+ 
146+   return  (
147+     <div >
148+       <div >{bears }< / div > 
149+       < button  onClick = {() => increase(5)}> 
150+         Increase  by  5 
151+       < / button > 
152+       < button  onClick = {reset }> 
153+         Reset 
154+       < / button > 
155+     < / div > 
156+   )
157+ }
157158``` 
158159
159160### Extracting Types  
@@ -224,12 +225,25 @@ Sometimes you need more than one property. Returning an object from the selector
224225This is more efficient than subscribing to the whole store. TypeScript ensures you can’t accidentally misspell ` bears `  or ` food ` .
225226
226227``` ts 
227- import  { useBearStore  } from  ' ./store ' 
228+ import  { create  } from  ' zustand ' 
228229
229- const =  useBearStore ((s ) =>  ({
230-   bears: s .bears ,
231-   food: s .food ,
230+ //  Bear store with explicit types
231+ interface  BearState  {
232+   bears:  number 
233+   food:  number 
234+ }
235+ 
236+ const =  create <BearState >()(() =>  ({
237+   bears: 2 ,
238+   food: 10 
232239}))
240+ 
241+ //  In components, you can use both stores safely
242+ function  MultipleSelectors() {
243+   const =  useBearStore ()
244+ 
245+   return  <div >We  have  {food } units  of  food  for  {bears } bears < / div > 
246+ }
233247``` 
234248
235249#### Derived State with Selectors  
@@ -238,21 +252,24 @@ Not all values need to be stored directly - some can be computed from existing s
238252This avoids duplication and keeps the store minimal. TypeScript ensures ` bears `  is a number, so math is safe.
239253
240254``` ts 
255+ import  { create  } from  ' zustand' 
256+ 
241257interface  BearState  {
242258  bears:  number 
243259  foodPerBear:  number 
244260}
245261
246- export   const =  create <BearState >(() =>  ({
262+ const =  create <BearState >() (() =>  ({
247263  bears: 3 ,
248264  foodPerBear: 2 ,
249265}))
250266
251- //  Derived value: required amount food for all bears
252- const =  useBearStore ((s ) =>  s .bears  *  s .foodPerBear ) //  don't need to have extra property `{ totalFood: 6 }` in your Store
267+ function  TotalFood() {
268+   //  Derived value: required amount food for all bears
269+   const =  useBearStore ((s ) =>  s .bears  *  s .foodPerBear ) //  don't need to have extra property `{ totalFood: 6 }` in your Store
253270
254- //  Usage 
255- console . log ( ` We need ${ totalFood } jars of honey ` ) 
271+    return  < div > We   need   $ { totalFood }  jars   of   honey < / div > 
272+ } 
256273``` 
257274
258275### Middlewares  
@@ -342,7 +359,7 @@ interface BearState {
342359  fetchBears:  () =>  Promise <void >
343360}
344361
345- export const =  create <BearState >((set ) =>  ({
362+ export const =  create <BearState >()( (set ) =>  ({
346363  bears: 0 ,
347364  fetchBears : async  () =>  {
348365    const =  await  fetch (' /api/bears' 
@@ -367,7 +384,7 @@ interface BearState {
367384  bears:  number 
368385}
369386
370- const =  createStore <BearState >(( ) =>  ({ bears: 1  }))
387+ const =  createStore <BearState >()(( ) =>  ({ bears: 2  }))
371388
372389function  Zoo() {
373390  const =  useStore (bearStore , (s ) =>  s .bears )
@@ -402,7 +419,7 @@ interface BearState {
402419  increase:  () =>  void 
403420}
404421
405- const =  create <BearState >((set ) =>  ({
422+ const =  create <BearState >()( (set ) =>  ({
406423  bears: 5 ,
407424  location: " forest" 
408425  increase : () =>  set ((s ) =>  ({ bears: s .bears  +  1  })),
@@ -418,18 +435,20 @@ function BearAmount() {
418435  return  <p >There  are  {bears } bears ! < / p > 
419436}
420437
421- function  App () {
438+ function  OptimizedZoo () {
422439  const =  useBearStore ((s ) =>  s .increase )
423440
424441  return  (
425442    <> 
426443      < BearAmount  / > 
427-     < button  onClick = {increase }> Increase  bears < / button > 
428-   < button  onClick = {() => useBearStore.setState({ location : " mountains" > 
429-   Change  location 
430-   < / button > 
431-   < / > 
432- )
444+       < button  onClick = {increase }> 
445+         Increase  bears 
446+       < / button > 
447+       < button  onClick = {() => useBearStore.setState({ location : " mountains" > 
448+         Change  location 
449+       < / button > 
450+     < / > 
451+   )
433452}
434453``` 
435454
0 commit comments