Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main--components--…
Browse files Browse the repository at this point in the history
…zustand-computed
  • Loading branch information
chrisvander authored Aug 22, 2024
2 parents c7981ba + ba059e8 commit 9dfa989
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 52 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,24 @@ A fully-featured example can be found under the "example" directory.
Here's an example with the Immer middleware.

> [!WARNING]
> Types may not be as you expect when using Immer, as it derives the SetState type from the output of GetState, where `zustand-computed` makes SetState only allow the regular Store and the GetState return both the store and the computed store. To access the ComputedStore inside Immer, you will need to assert the `Store` type as `Store & ComputedStore`.
> Immer derives the SetState type from the output of GetState, where `zustand-computed` types SetState to allow only the regular Store and types GetState to return both the store and the computed store. To avoid this issue, you may need to apply Immer outside of `zustand-computed`. If `zustand-computed` must be outside of Immer, you will need to assert the `Store` type as `Store & ComputedStore`.
```ts
const useStore = create<Store>()(
devtools(
computed(
immer((set) => ({
count: 1,
inc: () =>
set((state) => {
// example with Immer middleware
state.count += 1
}),
dec: () => set((state) => ({ count: state.count - 1 })),
})),
computeState
immer(
computed(
(set) => ({
count: 1,
inc: () =>
set((state) => {
// example with Immer middleware
state.count += 1
}),
dec: () => set((state) => ({ count: state.count - 1 })),
}),
computeState
),
)
)
)
Expand All @@ -144,8 +146,8 @@ const useStore = create<Store, [["chrisvander/zustand-computed", ComputedStore]]

Other options include passing a `keys` array, which explicitly spell out the selectors which trigger re-computation. You can also pass a custom `equalityFn`, such as [fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal) instead of the default `zustand/shallow`.

[build-img]: https://github.com/chrisvander/zustand-computed/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/chrisvander/zustand-computed/actions/workflows/release.yml
[build-img]: https://github.com/chrisvander/zustand-computed/actions/workflows/ci.yml/badge.svg
[build-url]: https://github.com/chrisvander/zustand-computed/actions/workflows/ci.yml
[downloads-img]: https://img.shields.io/npm/dt/zustand-computed
[downloads-url]: https://www.npmtrends.com/zustand-computed
[npm-img]: https://img.shields.io/npm/v/zustand-computed
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default { extends: ["@commitlint/config-conventional"] };
export default { extends: ["@commitlint/config-conventional"] }
51 changes: 18 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
{
"name": "zustand-computed",
"version": "1.4.2",
"description": "A Zustand middleware to create computed states.",
"author": "chrisvander",
"license": "MIT",
"repository": "chrisvander/zustand-computed",
"main": "dist/index.js",
"module": "dist/index.mjs",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.mjs"
}
},
"repository": "chrisvander/zustand-computed",
"files": [
"/dist"
],
"scripts": {
"check": "biome check --write",
"format": "biome format --write",
"lint": "biome lint",
"build": "vite build"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@commitlint/cli": "^19.4.0",
"@commitlint/config-conventional": "^19.2.2",
"@tsconfig/recommended": "^1.0.7",
"@types/jest": "^29.5.12",
"@tsconfig/bun": "^1.0.7",
"@types/bun": "^1.1.6",
"@types/node": "^22.5.0",
"husky": "^9.1.5",
"react": "^18.3.1",
Expand All @@ -41,18 +24,20 @@
"react": "^18.2.0",
"zustand": "^4.3.8"
},
"keywords": [
"zustand",
"computed",
"calculated",
"state",
"react",
"plugin",
"middleware",
"npm",
"typescript"
],
"dependencies": {
"@tsconfig/bun": "^1.0.7"
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.mjs"
}
},
"description": "A Zustand middleware to create computed states.",
"files": ["/dist"],
"keywords": ["zustand", "computed", "calculated", "state", "react", "plugin", "middleware", "npm", "typescript"],
"license": "MIT",
"scripts": {
"check": "biome check --write",
"format": "biome format --write",
"lint": "biome lint",
"build": "vite build"
}
}
9 changes: 5 additions & 4 deletions src/computed.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test, beforeEach, mock } from "bun:test"
import { type StateCreator, create } from "zustand"
import { type ComputedStateOpts, computed } from "./computed"

Expand Down Expand Up @@ -28,7 +29,7 @@ function computeState(state: Store): ComputedStore {
}

describe("default config", () => {
const computeStateMock = jest.fn(computeState)
const computeStateMock = mock(computeState)
const makeStore = () =>
create<Store, [["chrisvander/zustand-computed", ComputedStore]]>(
computed(
Expand All @@ -45,7 +46,7 @@ describe("default config", () => {

let useStore: ReturnType<typeof makeStore>
beforeEach(() => {
jest.clearAllMocks()
computeStateMock.mockClear()
useStore = makeStore()
})

Expand Down Expand Up @@ -86,7 +87,7 @@ describe("default config", () => {
})

describe("custom config", () => {
const computeStateMock = jest.fn(computeState)
const computeStateMock = mock(computeState)
const makeStore = (opts?: ComputedStateOpts<Store>) =>
create<Store, [["chrisvander/zustand-computed", ComputedStore]]>(
computed(
Expand Down Expand Up @@ -147,7 +148,7 @@ function computeSlice(state: CountSlice): ComputedStore {
}

describe("slices pattern", () => {
const computeSliceMock = jest.fn(computeSlice)
const computeSliceMock = mock(computeSlice)
const makeStore = () => {
const createCountSlice: StateCreator<
Store,
Expand Down

0 comments on commit 9dfa989

Please sign in to comment.