Skip to content
Open
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1d34a5e
initial commit
Henry8192 Jun 22, 2025
a8e70ec
update Zustand store docs naming conventions & creation
Henry8192 Jun 26, 2025
4598dc9
add zustand slicing and store usage docs.
Henry8192 Jun 26, 2025
8c8be4d
Apply suggestions from code review
Henry8192 Jun 27, 2025
22526a6
patch previous commit
Henry8192 Jun 27, 2025
2159be2
apply the rest of suggestions
Henry8192 Jun 28, 2025
f92f375
move state values section
Henry8192 Jun 28, 2025
56a46b5
address coderabbit review
Henry8192 Jun 28, 2025
678c4cf
Apply suggestions from code review
Henry8192 Jun 30, 2025
8f96f8e
remove extra spaces
Henry8192 Jun 30, 2025
f5f00b7
Merge branch 'main' into zustand-store-docs
junhaoliao Jun 30, 2025
f5822d6
Update examples
Henry8192 Jun 30, 2025
d02feb6
address comment
Henry8192 Jul 8, 2025
08e9a07
specify when to slice
Henry8192 Jul 9, 2025
19c7959
Apply suggestions from code review
Henry8192 Jul 10, 2025
3dbb7ad
Merge branch 'main' into zustand-store-docs
Henry8192 Aug 11, 2025
c97fa29
Merge branch 'main' into zustand-store-docs
Henry8192 Aug 22, 2025
da469a6
Merge branch 'main' into zustand-store-docs
junhaoliao Sep 2, 2025
eb38e38
Merge branch 'main' into zustand-store-docs
junhaoliao Sep 12, 2025
e5842c4
docs: Update Zustand store naming and structure guidelines for clarit…
junhaoliao Sep 13, 2025
31faf0a
Merge branch 'main' into zustand-store-docs
Henry8192 Sep 17, 2025
515406c
address review
Henry8192 Sep 19, 2025
edfbb8c
Merge branch 'main' into zustand-store-docs
Henry8192 Oct 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/src/dev-guide/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,61 @@ To avoid including a state variable in a React Hook's dependency array, you can
the state variable with an additional `Ref` suffix. E.g., `logEventNumRef` is the reference variable
that corresponds to the `logEventNum` state variable.

# Zustand

## File Naming

When creating Zustand stores, we follow these naming conventions:

* The store file name should be `xxxStore.ts`, where xxx is the name of the store in camelCase.
* If the file is too large and needs to be sliced, set the folder name to `xxxStore` and the store
creation file name to `index.ts`.

## Creation

### Values and actions

Zustand stores two types of data: values, which are state variables,
and actions, which are functions that modify these states.

We split values and actions in two different interfaces during the definition:
`XxxValues` for values and `XxxActions` for actions, both in PascalCase.
Then we combine them with a type called `XxxState` for store creation. Here's a real world example:

```ts
interface LogExportValues {
exportProgress: Nullable<number>;
}

interface LogExportActions {
setExportProgress: (newProgress: Nullable<number>) => void;
}

type LogExportState = LogExportValues & LogExportActions;
```

### Default values

We define default values for state variables during the creation.
The default values should be in CAPITAL_SNAKE_CASE. Here's the continuation of the previous example:

```ts
const LOG_EXPORT_STORE_DEFAULT: LogExportValues = {
exportProgress: null,
};
```

Notice that we define `LOG_EXPORT_STORE_DEFAULT` using `LogExportValues` interface, which can help
type check the default values.

### Actions Naming

When implementing Zustand store actions, we follow these naming conventions:

* Use `setXXX` for actions that simply change the value of a state.
* Use `updateXXX` for actions that do more than simply changing a value (e.g., perform additional
logic, make API calls, update multiple states).

[eslint-config-mjs]: https://github.com/y-scope/yscope-log-viewer/blob/main/eslint.config.mjs
[vite-worker-query-suffix]: https://vite.dev/guide/features.html#import-with-query-suffixes
[yscope-guidelines]: https://docs.yscope.com/dev-guide/contrib-guides-overview.html