Skip to content

Commit

Permalink
feat: use deep equal for state compare
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Feb 23, 2024
1 parent 5dd12cc commit c355b12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@
"engines": {
"node": ">=14"
},
"dependencies": {},
"peerDependencies": {
"history": ">=4",
"immer": ">=9",
"next": ">=11",
"react": ">=16.8",
"react-dom": ">=16.8",
"zustand": ">=4.4.0",
"type-fest": ">=4"
"type-fest": ">=4",
"fast-deep-equal": "^3"
},
"peerDependenciesMeta": {
"next": {
Expand Down Expand Up @@ -184,7 +184,8 @@
"typedoc": "^0.25.0",
"typescript": "^5.2.2",
"webpack": "^5.75.0",
"zustand": "^4.4.1"
"zustand": "^4.4.1",
"fast-deep-equal": "^3.1.3"
},
"resolutions": {
"lodash": "^4.17.21",
Expand Down
9 changes: 5 additions & 4 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Mutate, StateCreator, StoreApi, UseBoundStore } from 'zustand'
import { devtools, subscribeWithSelector } from 'zustand/middleware'
import { createWithEqualityFn } from 'zustand/traditional'
import isDeepEqual from 'fast-deep-equal'

import { shallow } from 'zustand/shallow'
import {
Expand Down Expand Up @@ -219,10 +220,10 @@ export const factoryParameters = <
initialRegisterState.values
)
if (
initialRegisterState.values !== values ||
initialRegisterState.initialValues !== initialValues ||
currentState.initialValues !== initialValues ||
currentState.values !== values
!isDeepEqual(initialRegisterState.values, values) ||
!isDeepEqual(initialRegisterState.initialValues, initialValues) ||
!isDeepEqual(currentState.initialValues, initialValues) ||
!isDeepEqual(currentState.values, values)
) {
setCurrentState({ values, initialValues })
}
Expand Down

0 comments on commit c355b12

Please sign in to comment.