Skip to content

Commit e4d435c

Browse files
authored
Merge pull request #115 from RavenColEvol/feat-set-callback-support
2 parents 101940b + 6cd25fe commit e4d435c

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

.changeset/sweet-buses-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"zustand-x": patch
3+
---
4+
5+
fix: add support for set callback support

.changeset/tall-brooms-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'zustand-x': patch
3+
---
4+
5+
fix support for set callback value

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"eslint-plugin-unicorn": "^49.0.0",
8989
"eslint-plugin-unused-imports": "^3.0.0",
9090
"eslint-plugin-vitest": "0.5.4",
91+
"glob": "8.1.0",
9192
"jsdom": "26.0.0",
9293
"prettier": "^3.1.0",
9394
"react": "^18.2.0",

packages/zustand-x/src/createStore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ export const createStore = <
149149
const typedKey = key as keyof StateType;
150150
const prevValue = store.getState()[typedKey];
151151

152+
if (typeof value === 'function') {
153+
value = value(prevValue);
154+
}
152155
if (prevValue === value) return;
153156

154157
const actionKey = key.replace(/^\S/, (s) => s.toUpperCase());

packages/zustand-x/src/tests/createStore.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ describe('zustandX', () => {
285285
validName: (name: string) => {
286286
set('name', name.trim());
287287
},
288+
validNameWithCallback: (lastName: string) => {
289+
set('name', (firstName) => firstName + ' ' + lastName);
290+
},
288291
replace2ndTag: (tag: string) => {
289292
set('state', (draft) => {
290293
draft.tags?.splice(1, 1, tag);
@@ -311,6 +314,11 @@ describe('zustandX', () => {
311314
expect(store.get('name')).toBe('test');
312315
});
313316

317+
it('should execute action with callback', () => {
318+
store.actions.validNameWithCallback('test');
319+
expect(store.get('name')).toBe('test test');
320+
});
321+
314322
it('should execute action with immer', () => {
315323
store.actions.replace2ndTag('tag3');
316324
expect(store.get('tags')).toEqual(['tag1', 'tag3']);

packages/zustand-x/src/types/store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export type TStoreApiSet<
4949
key: K,
5050
...args: Parameters<TActions[K]>
5151
): ReturnType<TActions[K]>;
52+
<K extends keyof StateType>(
53+
key: K,
54+
callback: (prevVal: StateType[K]) => StateType[K]
55+
): void;
5256
(
5357
key: 'state',
5458
value: Parameters<TCreatedStoreType<StateType, Mutators>['setState']>[0]

yarn.lock

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,6 +5098,19 @@ __metadata:
50985098
languageName: node
50995099
linkType: hard
51005100

5101+
"glob@npm:8.1.0, glob@npm:^8.0.0, glob@npm:^8.0.3":
5102+
version: 8.1.0
5103+
resolution: "glob@npm:8.1.0"
5104+
dependencies:
5105+
fs.realpath: "npm:^1.0.0"
5106+
inflight: "npm:^1.0.4"
5107+
inherits: "npm:2"
5108+
minimatch: "npm:^5.0.1"
5109+
once: "npm:^1.3.0"
5110+
checksum: 10c0/cb0b5cab17a59c57299376abe5646c7070f8acb89df5595b492dba3bfb43d301a46c01e5695f01154e6553168207cb60d4eaf07d3be4bc3eb9b0457c5c561d0f
5111+
languageName: node
5112+
linkType: hard
5113+
51015114
"glob@npm:^10.2.2, glob@npm:^10.3.10, glob@npm:^10.3.7":
51025115
version: 10.3.10
51035116
resolution: "glob@npm:10.3.10"
@@ -5127,19 +5140,6 @@ __metadata:
51275140
languageName: node
51285141
linkType: hard
51295142

5130-
"glob@npm:^8.0.0, glob@npm:^8.0.3":
5131-
version: 8.1.0
5132-
resolution: "glob@npm:8.1.0"
5133-
dependencies:
5134-
fs.realpath: "npm:^1.0.0"
5135-
inflight: "npm:^1.0.4"
5136-
inherits: "npm:2"
5137-
minimatch: "npm:^5.0.1"
5138-
once: "npm:^1.3.0"
5139-
checksum: 10c0/cb0b5cab17a59c57299376abe5646c7070f8acb89df5595b492dba3bfb43d301a46c01e5695f01154e6553168207cb60d4eaf07d3be4bc3eb9b0457c5c561d0f
5140-
languageName: node
5141-
linkType: hard
5142-
51435143
"glob@npm:^9.3.1":
51445144
version: 9.3.5
51455145
resolution: "glob@npm:9.3.5"
@@ -8802,6 +8802,7 @@ __metadata:
88028802
eslint-plugin-unicorn: "npm:^49.0.0"
88038803
eslint-plugin-unused-imports: "npm:^3.0.0"
88048804
eslint-plugin-vitest: "npm:0.5.4"
8805+
glob: "npm:8.1.0"
88058806
jsdom: "npm:26.0.0"
88068807
prettier: "npm:^3.1.0"
88078808
react: "npm:^18.2.0"

0 commit comments

Comments
 (0)