Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs content #2508

Draft
wants to merge 37 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9b23d94
Move docs to old docs
dbritto-dev Apr 24, 2024
b92ba68
Add new docs (WIP)
dbritto-dev Apr 24, 2024
e1c081e
Minor changes
dbritto-dev Apr 24, 2024
8828d67
Minor changes
dbritto-dev Apr 24, 2024
54344d1
Minor fixes
dbritto-dev Apr 24, 2024
90c23c0
Minor fixes
dbritto-dev Apr 24, 2024
aafed40
Minor fixes
dbritto-dev Apr 24, 2024
3144ea8
Minor fixes
dbritto-dev Apr 24, 2024
f1a3834
Minor changes
dbritto-dev Apr 24, 2024
aeb7cde
Minor fixes
dbritto-dev Apr 24, 2024
6c07e5f
Minor fixes
dbritto-dev Apr 24, 2024
60846c7
Minor changes
dbritto-dev Apr 25, 2024
7bdf1cc
Minor changes
dbritto-dev Apr 25, 2024
a4ab750
Minor changes
dbritto-dev Apr 25, 2024
1606977
Minor changes
dbritto-dev Apr 25, 2024
df190b9
Minor changes
dbritto-dev Apr 25, 2024
a5ceba7
Minor changes
dbritto-dev Apr 25, 2024
3637fe7
Minor fixes
dbritto-dev Apr 25, 2024
0746ab1
WIP
dbritto-dev Apr 25, 2024
adec185
Minor changes
dbritto-dev Apr 25, 2024
ee07ba5
WIP
dbritto-dev Apr 27, 2024
626e972
Merge branch 'main' of github.com:dbritto-dev/zustand into update-doc…
dbritto-dev Apr 27, 2024
89771e5
WIP
dbritto-dev Apr 28, 2024
72fec5c
WIP
dbritto-dev Apr 30, 2024
b1f84f8
WIP
dbritto-dev Apr 30, 2024
c4322c5
WIP
dbritto-dev Apr 30, 2024
5834bbf
WIP
dbritto-dev May 3, 2024
cdba318
WIP
dbritto-dev May 4, 2024
ac441ae
WIP
dbritto-dev May 6, 2024
aea4a45
WIP
dbritto-dev May 6, 2024
479da65
WIP
dbritto-dev May 9, 2024
135bc69
WIP
dbritto-dev May 16, 2024
17d433d
WIP
dbritto-dev May 31, 2024
c542900
WIP
dbritto-dev May 31, 2024
c3998cb
Update docs/reference/use-shallow.md
dbritto-dev Jun 2, 2024
27d8afd
Update docs/reference/create-with-equality-fn.md
dbritto-dev Jun 2, 2024
3322bea
WIP
dbritto-dev Jun 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/community/docs-contributors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Docs Contributors
description:
nav: 301
---
5 changes: 5 additions & 0 deletions docs/community/third-party-libraries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Third-party Libraries
description:
nav: 302
---
5 changes: 5 additions & 0 deletions docs/community/versioning-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Versioning Policy
description:
nav: 303
---
5 changes: 5 additions & 0 deletions docs/community/zustand-videos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Zustand Videos
description:
nav: 300
---
5 changes: 5 additions & 0 deletions docs/learn/async-data-and-data-fetching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Async Data and Data Fetching
description:
nav: 106
---
5 changes: 5 additions & 0 deletions docs/learn/choosing-the-state-structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Choosing the State Structure
description: One store, multiple stores, and multiple slices
nav: 104
---
5 changes: 5 additions & 0 deletions docs/learn/colocation-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Colocating Actions
description:
nav: 105
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Extracting State Logic into a Reducer with Redux and Devtools Middlewares
description:
nav: 112
---
5 changes: 5 additions & 0 deletions docs/learn/isolating-state-between-components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Isolating State Between Components
description:
nav: 108
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Listening to Selected Data with SubscribeWithSelector Middleware
description:
nav: 113
---
5 changes: 5 additions & 0 deletions docs/learn/middleware-use-cases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Middleware Use Cases
description:
nav: 114
---
Empty file added docs/learn/nextjs.md
Empty file.
5 changes: 5 additions & 0 deletions docs/learn/persisting-state-with-persist-middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Persisting State with Persist Middleware
description:
nav: 111
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Selecting Data and Preventing Re-renders with selectors and equality function
description:
nav: 107
---
5 changes: 5 additions & 0 deletions docs/learn/start-new-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Start a new project
description: Vanilla, React, and React Native
nav: 102
---
Empty file added docs/learn/testing.md
Empty file.
5 changes: 5 additions & 0 deletions docs/learn/thinking-in-zustand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Thinking in Zustand
description: Motivation, principles, and glossary
nav: 101
---
5 changes: 5 additions & 0 deletions docs/learn/tutorial-tic-tac-toe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: 'Tutorial: Tic-Tac-Toe'
description:
nav: 100
---
5 changes: 5 additions & 0 deletions docs/learn/using-middlewares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Using Middlewares
description:
nav: 109
---
5 changes: 5 additions & 0 deletions docs/learn/using-typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Using TypeScript
description:
nav: 103
---
5 changes: 5 additions & 0 deletions docs/learn/writing-immutable-update-with-immer-middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Writing Immutable Updates with Immer Middleware
description:
nav: 110
---
106 changes: 106 additions & 0 deletions docs/reference/combine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: combine
description:
nav: 200
---

# combine

`combine` lets you create a new state by combining an initial state with additional state created
by a function. This middleware is particularly useful due to infers the state so you do not need to
type it.

```js
combine(initialState, additionalStateCreator)
```

- [Reference](#reference)
- [Signature](#combine-signature)
- [`setState` function](#setstate-function)
- [`getState` function](#getstate-function)
- [`subscribe` function](#subscribe-function)
- [`destroy` function](#destroy-function)
- [`storeApi`](#storeapi)
- [Usage](#usage)
- TBD
- [Troubleshooting](#troubleshooting)
- TBD

## Reference

### `combine` Signature

```ts [TypeScript]
combine<T, U>(initialState: T, additionalStateCreator: StateCreator<T, [], [], U>): Omit<T, keyof U> & U
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we clarify where we can import this from? combine is from zustand/middleware.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a usage section where we can put it

```

#### Parameters

- `initialState`: The value you want the state to be initially. It can be a value of any type,
except a function.
- `additionalStateCreator`: A function that should take [`setState`](#setstate-function) function,
[`getState`](#getstate-function) function and [`api`](#api) as arguments. It should returns an
additional state based on the initial state.

#### Returns

`combine` returns a new state that is the result of merging the initial state and the additional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More precisely, combine and any other middleware "returns" a state creator.

state.

### `setState` function

The `setState` function lets you update the state to a different value and trigger a re-render. You
can pass the next state directly, a next partial state, a function that calculates it from the
previous state, or replace it completely.

#### Parameters

- `nextState`: The value that you want the state to be. It can be a value of any type, but there is
a special behavior for functions.
- if you pass an object as a `nextState`. It will shallow merge `nextState` with the current
state. You can pass only the properties you want to update, this allows for selective state
updates without modifying other properties.
- if you pass a non-object as a `nextState`, make sure you use `replace` as `true` to avoid
unexpected behaviors.
- if you pass a function as a `nextState`. It must be pure, should take current state as its
only argument, and should return the next state. The next state returned by the updater
function face the same restrictions of any next state.
- `replace`: This optional boolean flag controls whether to replace the entire state or merge the
update with the current state.

#### Returns

`setState` function do not have a return value.

### `getState` function

The `getState` function lets you access to the current state. It can be stale on async operations.

### `subscribe` function

The `subscribe` function lets you subscribe to state updates. It should take current state and
previous state as arguments.

#### Parameters

- `currentState`: It's the current state.
- `previousState`: It's the previous state.

#### Returns

`subscribe` returns a function that lets you unsubscribe.

### `destroy` function

The `destroy` function lets you clear all the listeners. This function is **deprecated** and would
be removed in the future.

### `storeApi`

The `storeApi` lets you access to the store api functions like [`setState`](#setstate-function)
function, [`getState`](#getstate-function) function, [`subscribe`](#subscribe-function) function,
and [`destroy`](#destroy-function) function.

## Usage

## Troubleshooting
24 changes: 24 additions & 0 deletions docs/reference/create-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: createContext
dbritto-dev marked this conversation as resolved.
Show resolved Hide resolved
description:
nav: 201
---

# createContext

`createContext` lets you create a context that components can provider or read. Lorem ipsum dolor,
sit amet consectetur adipisicing elit. Voluptate a ipsum in accusamus adipisci consectetur
aspernatur pariatur excepturi nam quod architecto id doloremque repellat nisi minus deserunt, quae
aperiam quasi.

```js
createContext()
```

## Reference

### `createContext` Signature

```ts
createContext<StoreApi<T>>(): { Provider: (props: any) => JSX.Element, useStore: () => T, useStoreApi: () => StoreApi<T> }
```
141 changes: 141 additions & 0 deletions docs/reference/create-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: createStore
description:
nav: 202
---

# createStore

`createStore` lets you create a vanilla store. You can use a vanilla store in React with
[`useStore`](./use-store) hook.

```js [JavaScript]
createStore(initializer)
```

- [Reference](#reference)
- [Signature](#createstore-signature)
- [`setState` function](#setstate-function)
- [`getState` function](#getstate-function)
- [`subscribe` function](#subscribe-function)
- [`destroy` function](#destroy-function)
- [`storeApi`](#storeapi)
- [Usage](#usage)
- [Updating state based on previous state](#updating-state-base-on-a-previous-state)
- [Updating objects and non-objects in state](#updating-objects-and-non-objects-in-state)
- [Subscribing to state updates](#subscribing-to-state-updates)
- [Split state in multiple slices](#split-state-in-multiple-slices)
- [Troubleshooting](#troubleshooting)
- [I’ve updated the state, but the screen doesn’t update](#ive-updated-the-state-but-the-screen-doesnt-update)

## Reference

### `createStore` Signature

```ts
createStore<T>()(initializer: StateCreator<T, [], []>): StoreApi<T>
```

#### Parameters

- `initializer`: The value you want the state to be initially. It can be a value of any type, but
when you pass a function should take [`setState`](#setstate-function) function,
[`getState`](#getstate-function) function and [`storeApi`](#storeapi) as arguments.

#### Returns

`createStore` returns a vanilla store, that expose [`setState`](#setstate-function) function,
[`getState`](#getstate-function) function, [`subscribe`](#subscribe-function) function, and
[`destroy`](#destroy-function) function.

### `setState` function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like such functions like setState should be in one place, with hyperlinks to them from, e.g., combine function to avoid copying.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sewera that's not supported with the current setup but when we migrate to vitepress we can move everything in one place and then just import them


The `setState` function lets you update the state to a different value and trigger a re-render. You
can pass the next state directly, a next partial state, a function that calculates it from the
previous state, or replace it completely.

#### Parameters

- `nextState`: The value that you want the state to be. It can be a value of any type, but there is
a special behavior for functions.
- if you pass an object as a `nextState`. It will shallow merge `nextState` with the current
state. You can pass only the properties you want to update, this allows for selective state
updates without modifying other properties.
- if you pass a non-object as a `nextState`, make sure you use `replace` as `true` to avoid
unexpected behaviors.
- if you pass a function as a `nextState`. It must be pure, should take current state as its
only argument, and should return the next state. The next state returned by the updater
function face the same restrictions of any next state.
- `replace`: This optional boolean flag controls whether to replace the entire state or merge the
update with the current state.

#### Returns

`setState` function do not have a return value.

### `getState` function

The `getState` function lets you access to the current state. It can be stale on async operations.

### `subscribe` function

The `subscribe` function lets you subscribe to state updates. It should take current state and
previous state as arguments.

#### Parameters

- `currentState`: It's the current state.
- `previousState`: It's the previous state.

#### Returns

`subscribe` returns a function that lets you unsubscribe.

### `destroy` function

The `destroy` function lets you clear all the listeners. This function is **deprecated** and would
be removed in the future.

### `storeApi`

The `storeApi` lets you access to the store api functions like [`setState`](#setstate-function)
function, [`getState`](#getstate-function) function, [`subscribe`](#subscribe-function) function,
and [`destroy`](#destroy-function) function.

## Usage

### Updating state base on a previous state

Lorem ipsum dolor sit amet consectetur adipisicing elit. Nam in provident eius eaque modi,
architecto consequuntur nihil soluta, dolore ratione, deleniti voluptatum unde. Qui veritatis,
deleniti error vero ducimus sunt.

### Updating objects and non-objects in state

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cupiditate consectetur sequi repellendus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be written before merging?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will

culpa nesciunt dolores aut. Voluptas corrupti expedita temporibus doloribus, maxime dolor iure
suscipit adipisci hic cumque quasi officia?

### Subscribing to state updates

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut quidem est neque consequuntur,
dolorem eius, explicabo ullam voluptatibus ex corporis qui, quasi eum reprehenderit maxime! Magni
magnam dignissimos eos dicta!

### Split state in multiple slices

Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellat, sint doloremque incidunt beatae
asperiores tempore amet quam ipsa commodi adipisci nobis quis fugiat aliquam? Facere repellendus
asperiores incidunt maxime facilis?

## Troubleshooting

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Aut illo, earum beatae voluptate corporis
saepe ipsa? Placeat animi commodi qui odit debitis eveniet enim maiores, illum tempora repellendus
sint non?

### I’ve updated the state, but the screen doesn’t update

Lorem ipsum dolor sit amet consectetur adipisicing elit. Modi facere vel minus saepe inventore hic
tenetur aspernatur officia ipsam accusantium illum, neque consectetur placeat doloremque pariatur
voluptatum amet odio quos!