Skip to content

Commit 211f108

Browse files
authored
Update docs content (#2760)
* Update docs content * Update docs content * Update docs content * Update docs content * Update docs content * WIP * WIP * Update docs content * Update create.md * Update create-with-equality-fn.md * Update create-store.md * Update use-store.md * Update use-store-with-equality-fn.md
1 parent 055dcdc commit 211f108

13 files changed

+2096
-283
lines changed

docs/apis/create-store.md

+56-80
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,36 @@ nav: 24
77
`createStore` lets you create a vanilla store that exposes API utilities.
88

99
```js
10-
createStore(stateCreatorFn)
10+
const someStore = createStore(stateCreatorFn)
1111
```
1212

13-
- [Reference](#reference)
13+
- [Types](#types)
1414
- [Signature](#createstore-signature)
15+
- [Reference](#reference)
1516
- [Usage](#usage)
1617
- [Updating state based on previous state](#updating-state-based-on-previous-state)
1718
- [Updating Primitives in State](#updating-primitives-in-state)
1819
- [Updating Objects in State](#updating-objects-in-state)
1920
- [Updating Arrays in State](#updating-arrays-in-state)
20-
- [Updating state with no store actions](#updating-state-with-no-store-actions)
2121
- [Subscribing to state updates](#subscribing-to-state-updates)
2222
- [Troubleshooting](#troubleshooting)
2323
- [I’ve updated the state, but the screen doesn’t update](#ive-updated-the-state-but-the-screen-doesnt-update)
2424

25-
## Reference
25+
## Types
2626

27-
### `createStore` Signature
27+
### Signature
2828

2929
```ts
3030
createStore<T>()(stateCreatorFn: StateCreator<T, [], []>): StoreApi<T>
3131
```
3232

33+
## Reference
34+
35+
### `createStore(stateCreatorFn)`
36+
3337
#### Parameters
3438

35-
- `stateCreatorFn`: A function that takes `set` function, `get` function and `api` as arguments.
39+
- `stateCreatorFn`: A function that takes `set` function, `get` function and `store` as arguments.
3640
Usually, you will return an object with the methods you want to expose.
3741

3842
#### Returns
@@ -44,7 +48,7 @@ createStore<T>()(stateCreatorFn: StateCreator<T, [], []>): StoreApi<T>
4448

4549
### Updating state based on previous state
4650

47-
This example shows how you can support **updater functions** for your **actions**.
51+
This example shows how you can support **updater functions** within **actions**.
4852

4953
```tsx
5054
import { createStore } from 'zustand/vanilla'
@@ -63,11 +67,10 @@ type AgeStore = AgeStoreState & AgeStoreActions
6367

6468
const ageStore = createStore<AgeStore>()((set) => ({
6569
age: 42,
66-
setAge: (nextAge) => {
70+
setAge: (nextAge) =>
6771
set((state) => ({
6872
age: typeof nextAge === 'function' ? nextAge(state.age) : nextAge,
69-
}))
70-
},
73+
})),
7174
}))
7275

7376
function increment() {
@@ -135,10 +138,8 @@ $dotContainer.addEventListener('pointermove', (event) => {
135138
xStore.setState(event.clientX, true)
136139
})
137140

138-
const render: Parameters<typeof xStore.subscribe>[0] = (state) => {
139-
const position = { y: 0, x: state }
140-
141-
$dot.style.transform = `translate(${position.x}px, ${position.y}px)`
141+
const render: Parameters<typeof xStore.subscribe>[0] = (x) => {
142+
$dot.style.transform = `translate(${x}px, 0)`
142143
}
143144

144145
render(xStore.getInitialState(), xStore.getInitialState())
@@ -174,20 +175,17 @@ discards any existing nested data within the state.
174175
```ts
175176
import { createStore } from 'zustand/vanilla'
176177

177-
type PositionStoreState = { x: number; y: number }
178+
type PositionStoreState = { position: { x: number; y: number } }
178179

179180
type PositionStoreActions = {
180-
setPosition: (nextPosition: Partial<PositionStoreState>) => void
181+
setPosition: (nextPosition: PositionStoreState['position']) => void
181182
}
182183

183184
type PositionStore = PositionStoreState & PositionStoreActions
184185

185186
const positionStore = createStore<PositionStore>()((set) => ({
186-
x: 0,
187-
y: 0,
188-
setPosition: (nextPosition) => {
189-
set(nextPosition)
190-
},
187+
position: { x: 0, y: 0 },
188+
setPosition: (position) => set({ position }),
191189
}))
192190

193191
const $dotContainer = document.getElementById('dot-container') as HTMLDivElement
@@ -201,9 +199,7 @@ $dotContainer.addEventListener('pointermove', (event) => {
201199
})
202200

203201
const render: Parameters<typeof positionStore.subscribe>[0] = (state) => {
204-
const position = { x: state.x, y: state.y }
205-
206-
$dot.style.transform = `translate(${position.x}px, ${position.y}px)`
202+
$dot.style.transform = `translate(${state.position.x}px, ${state.position.y}px)`
207203
}
208204

209205
render(positionStore.getInitialState(), positionStore.getInitialState())
@@ -255,10 +251,8 @@ $dotContainer.addEventListener('pointermove', (event) => {
255251
positionStore.setState([event.clientX, event.clientY], true)
256252
})
257253

258-
const render: Parameters<typeof positionStore.subscribe>[0] = (state) => {
259-
const position = { x: state[0], y: state[1] }
260-
261-
$dot.style.transform = `translate(${position.x}px, ${position.y}px)`
254+
const render: Parameters<typeof positionStore.subscribe>[0] = ([x, y]) => {
255+
$dot.style.transform = `translate(${x}px, ${y}px)`
262256
}
263257

264258
render(positionStore.getInitialState(), positionStore.getInitialState())
@@ -288,20 +282,17 @@ updates. We can use `subscribe` for external state management.
288282
```ts
289283
import { createStore } from 'zustand/vanilla'
290284

291-
type PositionStoreState = { x: number; y: number }
285+
type PositionStoreState = { position: { x: number; y: number } }
292286

293287
type PositionStoreActions = {
294-
setPosition: (nextPosition: Partial<PositionStoreState>) => void
288+
setPosition: (nextPosition: PositionStoreState['position']) => void
295289
}
296290

297291
type PositionStore = PositionStoreState & PositionStoreActions
298292

299293
const positionStore = createStore<PositionStore>()((set) => ({
300-
x: 0,
301-
y: 0,
302-
setPosition: (nextPosition) => {
303-
set(nextPosition)
304-
},
294+
position: { x: 0, y: 0 },
295+
setPosition: (position) => set({ position }),
305296
}))
306297

307298
const $dot = document.getElementById('dot') as HTMLDivElement
@@ -318,17 +309,15 @@ $dot.addEventListener('mouseenter', (event) => {
318309
})
319310

320311
const render: Parameters<typeof positionStore.subscribe>[0] = (state) => {
321-
const position = { x: state.x, y: state.y }
322-
323-
$dot.style.transform = `translate(${position.x}px, ${position.y}px)`
312+
$dot.style.transform = `translate(${state.position.x}px, ${state.position.y}px)`
324313
}
325314

326315
render(positionStore.getInitialState(), positionStore.getInitialState())
327316

328317
positionStore.subscribe(render)
329318

330319
const logger: Parameters<typeof positionStore.subscribe>[0] = (state) => {
331-
console.log('new position', { position: { x: state.x, y: state.x } })
320+
console.log('new position', { position: state.position })
332321
}
333322

334323
positionStore.subscribe(logger)
@@ -363,24 +352,22 @@ These input fields don’t work because the `oninput` handlers mutate the state:
363352
import { createStore } from 'zustand/vanilla'
364353

365354
type PersonStoreState = {
366-
firstName: string
367-
lastName: string
368-
email: string
355+
person: { firstName: string; lastName: string; email: string }
369356
}
370357

371358
type PersonStoreActions = {
372-
setPerson: (nextPerson: Partial<PersonStoreState>) => void
359+
setPerson: (nextPerson: PersonStoreState['person']) => void
373360
}
374361

375362
type PersonStore = PersonStoreState & PersonStoreActions
376363

377364
const personStore = createStore<PersonStore>()((set) => ({
378-
firstName: 'Barbara',
379-
lastName: 'Hepworth',
380-
381-
setPerson: (nextPerson) => {
382-
set(nextPerson)
365+
person: {
366+
firstName: 'Barbara',
367+
lastName: 'Hepworth',
368+
383369
},
370+
setPerson: (person) => set({ person }),
384371
}))
385372

386373
const $firstNameInput = document.getElementById(
@@ -391,33 +378,27 @@ const $emailInput = document.getElementById('email') as HTMLInputElement
391378
const $result = document.getElementById('result') as HTMLDivElement
392379

393380
function handleFirstNameChange(event: Event) {
394-
personStore.getState().firstName = (event.target as any).value
381+
personStore.getState().person.firstName = (event.target as any).value
395382
}
396383

397384
function handleLastNameChange(event: Event) {
398-
personStore.getState().lastName = (event.target as any).value
385+
personStore.getState().person.lastName = (event.target as any).value
399386
}
400387

401388
function handleEmailChange(event: Event) {
402-
personStore.getState().email = (event.target as any).value
389+
personStore.getState().person.email = (event.target as any).value
403390
}
404391

405392
$firstNameInput.addEventListener('input', handleFirstNameChange)
406393
$lastNameInput.addEventListener('input', handleLastNameChange)
407394
$emailInput.addEventListener('input', handleEmailChange)
408395

409396
const render: Parameters<typeof personStore.subscribe>[0] = (state) => {
410-
const person = {
411-
firstName: state.firstName,
412-
lastName: state.lastName,
413-
email: state.email,
414-
}
397+
$firstNameInput.value = state.person.firstName
398+
$lastNameInput.value = state.person.lastName
399+
$emailInput.value = state.person.email
415400

416-
$firstNameInput.value = person.firstName
417-
$lastNameInput.value = person.lastName
418-
$emailInput.value = person.email
419-
420-
$result.innerHTML = `${person.firstName} ${person.lastName} (${person.email})`
401+
$result.innerHTML = `${state.person.firstName} ${state.person.lastName} (${state.person.email})`
421402
}
422403

423404
render(personStore.getInitialState(), personStore.getInitialState())
@@ -472,24 +453,22 @@ keeping all data grouped in an object is very convenient—as long as you update
472453
import { createStore } from 'zustand/vanilla'
473454

474455
type PersonStoreState = {
475-
firstName: string
476-
lastName: string
477-
email: string
456+
person: { firstName: string; lastName: string; email: string }
478457
}
479458

480459
type PersonStoreActions = {
481-
setPerson: (nextPerson: Partial<PersonStoreState>) => void
460+
setPerson: (nextPerson: PersonStoreState['person']) => void
482461
}
483462

484463
type PersonStore = PersonStoreState & PersonStoreActions
485464

486465
const personStore = createStore<PersonStore>()((set) => ({
487-
firstName: 'Barbara',
488-
lastName: 'Hepworth',
489-
490-
setPerson: (nextPerson) => {
491-
set(nextPerson)
466+
person: {
467+
firstName: 'Barbara',
468+
lastName: 'Hepworth',
469+
492470
},
471+
setPerson: (person) => set({ person }),
493472
}))
494473

495474
const $firstNameInput = document.getElementById(
@@ -501,18 +480,21 @@ const $result = document.getElementById('result') as HTMLDivElement
501480

502481
function handleFirstNameChange(event: Event) {
503482
personStore.getState().setPerson({
483+
...personStore.getState().person,
504484
firstName: (event.target as any).value,
505485
})
506486
}
507487

508488
function handleLastNameChange(event: Event) {
509489
personStore.getState().setPerson({
490+
...personStore.getState().person,
510491
lastName: (event.target as any).value,
511492
})
512493
}
513494

514495
function handleEmailChange(event: Event) {
515496
personStore.getState().setPerson({
497+
...personStore.getState().person,
516498
email: (event.target as any).value,
517499
})
518500
}
@@ -522,17 +504,11 @@ $lastNameInput.addEventListener('input', handleLastNameChange)
522504
$emailInput.addEventListener('input', handleEmailChange)
523505

524506
const render: Parameters<typeof personStore.subscribe>[0] = (state) => {
525-
const person = {
526-
firstName: state.firstName,
527-
lastName: state.lastName,
528-
email: state.email,
529-
}
530-
531-
$firstNameInput.value = person.firstName
532-
$lastNameInput.value = person.lastName
533-
$emailInput.value = person.email
507+
$firstNameInput.value = state.person.firstName
508+
$lastNameInput.value = state.person.lastName
509+
$emailInput.value = state.person.email
534510

535-
$result.innerHTML = `${person.firstName} ${person.lastName} (${person.email})`
511+
$result.innerHTML = `${state.person.firstName} ${state.person.lastName} (${state.person.email})`
536512
}
537513

538514
render(personStore.getInitialState(), personStore.getInitialState())

0 commit comments

Comments
 (0)