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

breaking(types): Add higher kinded mutator types #725

Merged
merged 250 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 199 commits
Commits
Show all changes
250 commits
Select commit Hold shift + click to select a range
cf511cf
imaginary code that uses uSES
dai-shi Aug 31, 2021
5570b10
revert backward compatibility code as this is not going to be v4
dai-shi Aug 31, 2021
b03b226
use use-sync-external-store
dai-shi Sep 4, 2021
8112285
revert to react 17
dai-shi Sep 4, 2021
50f2963
handle error by our own
dai-shi Sep 4, 2021
94d9d4a
v4.0.0-alpha.2
dai-shi Sep 4, 2021
d36611b
fix&refactor a bit
dai-shi Sep 6, 2021
453e59a
Merge branch 'main' into use-sync-external-store
dai-shi Sep 14, 2021
ef89673
update uSES experimental package
dai-shi Sep 14, 2021
d61a0b7
remove error propagation hack
dai-shi Sep 14, 2021
ec117e6
update size snapshot
dai-shi Sep 14, 2021
e2081c8
update uSES and add dts
dai-shi Sep 27, 2021
1fee70e
split react.ts and no export wild
dai-shi Sep 27, 2021
9aaeb5e
split useStore impl
dai-shi Sep 27, 2021
a914052
context to follow the new api, export wild again
dai-shi Sep 27, 2021
9021456
v4.0.0-alpha.3
dai-shi Sep 27, 2021
b9a70f2
add missing await
dai-shi Sep 28, 2021
98c59d3
merge main
dai-shi Sep 28, 2021
c9c8ae9
merge main
dai-shi Oct 1, 2021
cc391bc
update uSES
dai-shi Oct 1, 2021
bef6f81
update uSES
dai-shi Oct 3, 2021
d78e91f
Merge branch 'main' into use-sync-external-store
dai-shi Oct 3, 2021
54a5b8c
uses uSES extra!
dai-shi Oct 4, 2021
66fc5a7
Merge branch 'main' into use-sync-external-store
dai-shi Oct 4, 2021
1ee93aa
v4.0.0-alpha.3
dai-shi Oct 4, 2021
c643ebf
merge main
dai-shi Oct 7, 2021
a20e4d1
merge main
dai-shi Oct 19, 2021
002d101
fix(types): Rename from UseStore to UseBoundStore
dai-shi Oct 19, 2021
674e307
breaking(types): drop deprecated UseStore type
dai-shi Oct 19, 2021
0c641fd
breaking(core): drop v2 hook compatibility
dai-shi Oct 19, 2021
0037eb3
breaking(middleware): drop deprecated persist options
dai-shi Oct 19, 2021
57d2b85
merge main
dai-shi Oct 21, 2021
56d512a
merge main
dai-shi Oct 21, 2021
9b97050
breaking(core): drop deprecated store.subscribe with selector
dai-shi Oct 21, 2021
b691632
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Oct 27, 2021
bedad91
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Oct 27, 2021
263fce4
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Oct 27, 2021
916f0b8
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Oct 27, 2021
3b03362
merge main
dai-shi Oct 27, 2021
24d9750
update uSES
dai-shi Oct 27, 2021
f75e868
fix update uSES
dai-shi Oct 27, 2021
2ed60eb
v4.0.0-alpha.5
dai-shi Oct 27, 2021
3bb2fef
combine subscribe type
dai-shi Oct 29, 2021
d3eec66
merge main
dai-shi Oct 30, 2021
5e062a6
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Oct 30, 2021
4d00841
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Oct 30, 2021
4c80f17
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Oct 30, 2021
9675378
merge main
dai-shi Oct 30, 2021
ba9d6d0
intentional undefined type
dai-shi Oct 31, 2021
8fad667
add useDebugValue
dai-shi Oct 31, 2021
7575357
update uSES
dai-shi Oct 31, 2021
e51f75a
update uSES types
dai-shi Nov 2, 2021
018f9b2
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Nov 2, 2021
9d7ea48
merge main
dai-shi Nov 2, 2021
ebf571e
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Nov 2, 2021
48684d2
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Nov 2, 2021
c001de4
breaking(middleware): make persist options.removeItem required
dai-shi Nov 2, 2021
7941091
merge main
dai-shi Nov 2, 2021
e7adbbf
update uSES
dai-shi Nov 2, 2021
5812d0e
v4.0.0-alpha.6
dai-shi Nov 2, 2021
19d40a6
merge main
dai-shi Nov 4, 2021
b669f7e
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Nov 4, 2021
3357bee
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Nov 4, 2021
dc0b449
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Nov 4, 2021
9690a11
merge main
dai-shi Nov 4, 2021
f037a2a
fix(readme): remove memoization section which is no longer valid with…
dai-shi Nov 4, 2021
8339d52
feat(readme): add new createStore/useStore usage
dai-shi Nov 4, 2021
258e895
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Nov 9, 2021
2392f8f
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Nov 9, 2021
707b37b
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Nov 9, 2021
de84ec8
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Nov 9, 2021
2e5ab21
merge main
dai-shi Nov 9, 2021
37119f5
update useSES
dai-shi Nov 9, 2021
835684a
update uSES and deps
dai-shi Nov 15, 2021
20abedf
Merge branch 'main' into use-sync-external-store
dai-shi Nov 15, 2021
e91bde6
v4.0.0-alpha.7
dai-shi Nov 15, 2021
7206e1a
merge main
dai-shi Nov 16, 2021
e8f7316
merge main
dai-shi Nov 16, 2021
6372ee8
merge main
dai-shi Nov 16, 2021
b5d8665
merge main
dai-shi Nov 16, 2021
c009886
merge main
dai-shi Nov 16, 2021
a0777aa
Merge branch 'breaking/middleware/drop-deprecated-persist-options' in…
dai-shi Nov 16, 2021
8d11b90
Merge branch 'breaking/drop-v2-hook-compatibility' into v4
dai-shi Nov 16, 2021
c972116
Merge branch 'breaking/types/drop-use-store' into v4
dai-shi Nov 16, 2021
cf56bf5
merege uses
dai-shi Nov 16, 2021
e87182b
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Nov 22, 2021
50022f9
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Nov 22, 2021
3c12d7d
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Nov 22, 2021
e675524
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Nov 22, 2021
be18127
merge main
dai-shi Nov 22, 2021
9280262
update uSES
dai-shi Nov 22, 2021
9fb629a
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Dec 3, 2021
e9d2ca3
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Dec 3, 2021
d6a4e42
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Dec 3, 2021
eddbf38
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Dec 3, 2021
c6acc59
merge main
dai-shi Dec 3, 2021
303fd82
update uSES
dai-shi Dec 7, 2021
0d4e0d8
shave bytes
dai-shi Dec 7, 2021
ef5e758
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Dec 7, 2021
eb1e686
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Dec 7, 2021
252fca0
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Dec 7, 2021
eee639e
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Dec 7, 2021
445bc8f
Merge branch 'main' into use-sync-external-store
dai-shi Dec 7, 2021
ba82ac2
Merge branch 'breaking/middleware/drop-deprecated-persist-options' in…
dai-shi Dec 7, 2021
adada31
Merge branch 'breaking/drop-v2-hook-compatibility' into v4
dai-shi Dec 7, 2021
1cf08fa
Merge branch 'breaking/types/drop-use-store' into v4
dai-shi Dec 7, 2021
fd47b39
Merge branch 'use-sync-external-store' into v4
dai-shi Dec 7, 2021
c99035d
Merge branch 'v4' of https:/pmndrs/zustand into v4
devanshj Dec 21, 2021
9d8009a
vanilla: add higher kinded mutator types
devanshj Dec 23, 2021
bcd6e0d
persist: add higher kinded mutator types
devanshj Dec 23, 2021
51ec8cf
persist: try to minimize diff
devanshj Dec 23, 2021
41bf7ea
use `PopArgument` in vanilla too
devanshj Dec 23, 2021
5a6473b
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Dec 24, 2021
3ac128f
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Dec 24, 2021
3818dc1
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Dec 24, 2021
e7a219d
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Dec 24, 2021
dd57cb0
Merge branch 'main' into use-sync-external-store
dai-shi Dec 24, 2021
76b1d4c
update uSES
dai-shi Dec 24, 2021
923c46f
Merge branch 'breaking/middleware/drop-deprecated-persist-options' in…
dai-shi Dec 24, 2021
be26ceb
Merge branch 'breaking/drop-v2-hook-compatibility' into v4
dai-shi Dec 24, 2021
d59dde1
Merge branch 'breaking/types/drop-use-store' into v4
dai-shi Dec 24, 2021
33d5940
Merge branch 'use-sync-external-store' into v4
dai-shi Dec 24, 2021
847334d
use overloads instead of `createWithState`
devanshj Jan 6, 2022
3e1beaa
avoid symbols
devanshj Jan 8, 2022
15b478f
add new types to middlewares
devanshj Jan 8, 2022
1bdaa39
add new types react
devanshj Jan 8, 2022
5eeeb3c
add new types to context
devanshj Jan 8, 2022
dcfb92f
fix persist types
devanshj Jan 8, 2022
b0ea17f
add immer
devanshj Jan 8, 2022
45b4317
migrate middleware type tests
devanshj Jan 8, 2022
40d8ce8
fix react type, export `UseBoundStore`
devanshj Jan 8, 2022
308112a
migrate vanilla type tests
devanshj Jan 8, 2022
9e2826b
merge v4
devanshj Jan 9, 2022
272e254
rename `_createStore` to `createStoreImpl`
devanshj Jan 9, 2022
c61e207
Default to no mutations in `StateCreator`
devanshj Jan 16, 2022
34d13b5
migrate context.test.tsx
devanshj Feb 9, 2022
9238443
fix devtools.test.tsx type erros
devanshj Feb 9, 2022
1bf8afd
context: remove callsignature in useStoreApi
devanshj Feb 9, 2022
b95fb26
context: remove `UseContextStore` type
devanshj Feb 9, 2022
cffa52a
context: fix useBoundStore type
devanshj Feb 9, 2022
5aa4bac
context: keep `UseContextStore` for tooltip just don't export it
devanshj Feb 9, 2022
594c7fa
react: remove duplicate overload in create
devanshj Feb 9, 2022
d38a259
export `WithPersist`
devanshj Feb 9, 2022
7a1ec6b
devtools: preserve try/catch
devanshj Feb 9, 2022
045ddb9
devtools: preserve window check
devanshj Feb 9, 2022
58d06bc
add a test case for v3 style create
devanshj Feb 9, 2022
5c2ac1a
devtools: preverse test fix from base branch
devanshj Feb 9, 2022
14ded44
remove StoreApiWithFoo types, don't export WithFoo types
devanshj Feb 9, 2022
0fac7b5
style
devanshj Feb 10, 2022
e2f4c74
devtools: preverse `originalIsRecording` change
devanshj Feb 10, 2022
970b0e2
fix bug in devtools
dai-shi Feb 11, 2022
c037c08
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Feb 11, 2022
bbbf857
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Feb 11, 2022
d3810d1
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Feb 11, 2022
82856c5
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Feb 11, 2022
90fd7ee
Merge branch 'main' into use-sync-external-store
dai-shi Feb 11, 2022
ad3b466
Merge branch 'breaking/middleware/drop-deprecated-persist-options' in…
dai-shi Feb 11, 2022
a0f186e
Merge branch 'breaking/drop-v2-hook-compatibility' into v4
dai-shi Feb 11, 2022
bc1e120
Merge branch 'breaking/types/drop-use-store' into v4
dai-shi Feb 11, 2022
742f76d
Merge branch 'use-sync-external-store' into v4
dai-shi Feb 11, 2022
8031d3f
empty commit
dai-shi Feb 11, 2022
ac2dcaa
4.0.0-beta.1
dai-shi Feb 11, 2022
01b42df
fix lint
dai-shi Feb 11, 2022
4e162d0
style
devanshj Feb 13, 2022
406b104
merge v4 branch
devanshj Feb 13, 2022
3d6d26f
export immer fix tests
devanshj Feb 13, 2022
36a92f7
style, minor fixes
devanshj Feb 17, 2022
82cc93a
devtools: fix test
devanshj Feb 17, 2022
3687f68
Update tests/devtools.test.tsx
dai-shi Feb 18, 2022
be51c33
breaking(middleware/devtools): use official devtools extension types
dai-shi Feb 22, 2022
06123b1
type object.create
dai-shi Feb 23, 2022
e9cf15e
avoid emitting @redux-devtools/extension
dai-shi Feb 23, 2022
4a30a95
fix type with any
dai-shi Feb 24, 2022
86d4f97
refactor
dai-shi Feb 24, 2022
4241357
Merge branch 'main' into breaking/middleware/devtools/use-devtools-ex…
dai-shi Feb 28, 2022
9b8fe3e
Merge branch 'main' into breaking/core/drop-deprecated-subscribe-with…
dai-shi Feb 28, 2022
5298a35
Merge branch 'main' into breaking/middleware/drop-deprecated-persist-…
dai-shi Feb 28, 2022
5ec8123
Merge branch 'main' into breaking/drop-v2-hook-compatibility
dai-shi Feb 28, 2022
37c4430
Merge branch 'main' into breaking/types/drop-use-store
dai-shi Feb 28, 2022
ff7358a
Merge branch 'main' into use-sync-external-store
dai-shi Feb 28, 2022
92a7094
fix yarn lock
dai-shi Feb 28, 2022
64536ee
temporary fix #829
dai-shi Feb 28, 2022
fede093
Merge branch 'breaking/core/drop-deprecated-subscribe-with-selector' …
dai-shi Feb 28, 2022
4be2722
Merge branch 'breaking/middleware/drop-deprecated-persist-options' in…
dai-shi Feb 28, 2022
12ab776
Merge branch 'breaking/drop-v2-hook-compatibility' into v4
dai-shi Feb 28, 2022
c79d27c
Merge branch 'breaking/types/drop-use-store' into v4
dai-shi Feb 28, 2022
d5f7fa7
Merge branch 'use-sync-external-store' into v4
dai-shi Feb 28, 2022
0c0f3a3
v4.0.0-beta.2
dai-shi Feb 28, 2022
304ee89
fix lint
dai-shi Feb 28, 2022
d4b7200
lock date-fns version
dai-shi Mar 4, 2022
48b0267
test middleware subtyping
devanshj Mar 5, 2022
d0820ee
Merge branch 'add-hkm-types' of https:/devanshj/zustand i…
devanshj Mar 5, 2022
1ffbd34
merge v4
devanshj Mar 5, 2022
b653b4b
fix errors in conflict resolution
devanshj Mar 5, 2022
129b64b
lock testing-library/react alpha version
dai-shi Mar 5, 2022
49f0a3a
Merge branch 'v4' of https:/pmndrs/zustand into add-hkm-t…
devanshj Mar 20, 2022
7f54b3c
more correct (and strict) persist types
devanshj Mar 20, 2022
414c398
migrate tests
devanshj Mar 20, 2022
155a177
wip release notes
devanshj Mar 21, 2022
b76e36c
fix devtools merge with base
devanshj Mar 27, 2022
719d184
add a test case for persist with partialize option
devanshj Mar 27, 2022
6198b31
Merge branch 'add-hkm-types' of https:/devanshj/zustand i…
devanshj Mar 27, 2022
c71afb6
update readme
devanshj Mar 30, 2022
826ef8e
fix lint
devanshj Mar 30, 2022
2873f22
immer: mutate `store.setState`
devanshj Mar 30, 2022
a539f54
fix devtools merge with base
devanshj Mar 30, 2022
26f04d4
immer: fix mutations order
devanshj Apr 3, 2022
968ca5d
changes in readme
devanshj Apr 3, 2022
7b4793f
move and rename v4 migration md
devanshj Apr 3, 2022
d635082
add `combine` usage in readme
devanshj Apr 3, 2022
810b418
typos
devanshj Apr 3, 2022
4f9d13f
create separate md for typescript, add common recipes
devanshj Apr 4, 2022
5424719
minor fixes
devanshj Apr 4, 2022
cb8c4f3
devtools: minor type fix
devanshj Apr 4, 2022
a328e04
add more migrations
devanshj Apr 4, 2022
57078dd
context: fix import
devanshj Apr 5, 2022
25bdc48
devtools: minor refactor
devanshj Apr 5, 2022
269638b
Merge branch 'add-hkm-types' of https:/devanshj/zustand i…
devanshj Apr 5, 2022
1f2041c
fix lint: run prettier
devanshj Apr 5, 2022
e927935
wip
devanshj Apr 5, 2022
7aaac47
uSES rc.1
dai-shi Mar 4, 2022
5a0b404
getServerState for #886, no types yet
dai-shi Mar 31, 2022
1a13c15
uSES v1
dai-shi Apr 5, 2022
87da744
devtools: remove deprecations and warnings
devanshj Apr 5, 2022
a01cf35
fix tests
devanshj Apr 5, 2022
dec16dc
v4.0.0-beta.2
dai-shi Apr 5, 2022
ba0660e
merge base
devanshj Apr 6, 2022
8cad3f6
wip
devanshj Apr 8, 2022
93aff9b
merge base ie main
devanshj Apr 8, 2022
aa1c0e2
migrate tests
devanshj Apr 8, 2022
ba702c1
persist: keep diff minimal
devanshj Apr 8, 2022
ee512ec
fix merge in package.json and yarn.lock
dai-shi Apr 8, 2022
efecba0
fix merge for persist
devanshj Apr 8, 2022
865f60b
Merge branch 'add-hkm-types' of https:/devanshj/zustand i…
devanshj Apr 8, 2022
0a4625b
don't use `import type`
devanshj Apr 9, 2022
160f11d
docs(typescript): add slices pattern
devanshj Apr 9, 2022
46589cc
Merge branch 'main' into add-hkm-types
dai-shi Apr 11, 2022
6ed70c3
fix selector & equals types
devanshj Apr 11, 2022
abf876a
Merge branch 'add-hkm-types' of https:/devanshj/zustand i…
devanshj Apr 11, 2022
061a057
add test for setState with replace
devanshj Apr 11, 2022
5477b3d
remove undefined selector overload
devanshj Apr 12, 2022
f0d192a
make immer more generic
devanshj Apr 11, 2022
58dc684
make devtools replace-friendly and more correctly typed
devanshj Apr 11, 2022
374a5f0
migrate tests
devanshj Apr 12, 2022
3063df1
make setState bivariant to make the state covariant
devanshj Apr 12, 2022
8a3b5db
devtools: return the result of `setState`
devanshj Apr 13, 2022
c4a6fe5
devtools: make the fallback branch in `StoreSetStateWithAction` bivar…
devanshj Apr 14, 2022
fb624ac
remove strict replace
devanshj Apr 14, 2022
4861161
fix lint
devanshj Apr 17, 2022
9d6e7e2
Merge branch 'main' into add-hkm-types
dai-shi Apr 17, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/test-multiple-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- run: yarn install --frozen-lockfile --check-files
- name: Install alpha testing-library
if: ${{ matrix.testing == 'alpha' }}
run: yarn add -D @testing-library/react@alpha
run: yarn add -D @testing-library/react@13.0.0-alpha.5
- name: Patch for React 16
if: ${{ startsWith(matrix.react, '16.') }}
run: |
Expand Down
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "zustand",
"private": true,
"version": "3.7.1",
"version": "4.0.0-beta.2",
"publishConfig": {
"tag": "next"
},
"description": "🐻 Bear necessities for state management in React",
"main": "./index.js",
"types": "./index.d.ts",
Expand Down Expand Up @@ -130,13 +133,17 @@
"tests/**/*.{js,ts,tsx}"
]
},
"dependencies": {
"use-sync-external-store": "1.0.0-rc.1-next-629036a9c-20220224"
},
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/plugin-external-helpers": "^7.16.7",
"@babel/plugin-transform-react-jsx": "^7.17.3",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/plugin-transform-typescript": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"@redux-devtools/extension": "^3.2.2",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-node-resolve": "^13.1.3",
"@rollup/plugin-replace": "^4.0.0",
Expand All @@ -147,6 +154,7 @@
"@types/jest": "^27.4.1",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@types/use-sync-external-store": "^0.0.3",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"concurrently": "^7.0.0",
Expand All @@ -167,12 +175,16 @@
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"redux": "^5.0.0-alpha.0",
"rollup": "^2.68.0",
"rollup-plugin-esbuild": "^4.8.2",
"rollup-plugin-terser": "^7.0.2",
"shx": "^0.3.4",
"typescript": "^4.5.5"
},
"resolutions": {
"date-fns": "2.27.0"
},
"peerDependencies": {
"react": ">=16.8"
},
Expand Down
46 changes: 28 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,6 @@ const treats = useStore(
)
```

## Memoizing selectors

It is generally recommended to memoize selectors with useCallback. This will prevent unnecessary computations each render. It also allows React to optimize performance in concurrent mode.

```jsx
const fruit = useStore(useCallback(state => state.fruits[id], [id]))
```

If a selector doesn't depend on scope, you can define it outside the render function to obtain a fixed reference without useCallback.

```jsx
const selector = state => state.berries

function Component() {
const berries = useStore(selector)
```

## Overwriting state

The `set` function has a second argument, `false` by default. Instead of merging, it will replace the state model. Be careful not to wipe out parts you rely on, like actions.
Expand Down Expand Up @@ -468,7 +451,33 @@ devtools(..., { anonymousActionType: 'unknown', ... })

## React context

The store created with `create` doesn't require context providers. In some cases, you may want to use contexts for dependency injection or if you want to initialize your store with props from a component. Because the store is a hook, passing it as a normal context value may violate rules of hooks. To avoid misusage, a special `createContext` is provided.
The store created with `create` doesn't require context providers. In some cases, you may want to use contexts for dependency injection or if you want to initialize your store with props from a component. Because the normal store is a hook, passing it as a normal context value may violate rules of hooks.

The flexible method available since v4 is to use vanilla store.

```jsx
import { createContext, useContext } from 'react'
import { createStore, useStore } from 'zustand'

const store = createStore(...) // vanilla store without hooks

const StoreContext = createContext()

const App = () => (
<StoreContext.Provider value={store}>
...
</StoreContext.Provider>
)

const Component = () => {
const store = useContext(StoreContext)
const slice = useStore(store, selector)
...
}
```

Alternatively, a special `createContext` is provided since v3.5,
which avoid misusing the store hook.

```jsx
import create from 'zustand'
Expand All @@ -490,6 +499,7 @@ const Component = () => {
...
}
```

<details>
<summary>createContext usage in real components</summary>

Expand Down
3 changes: 3 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function createESMConfig(input, output) {
resolve({ extensions }),
replace({
__DEV__: '(import.meta.env&&import.meta.env.MODE)!=="production"',
// a workround for #829
'use-sync-external-store/shim/with-selector':
'use-sync-external-store/shim/with-selector.js',
preventAssignment: true,
}),
getEsbuild('node12'),
Expand Down
91 changes: 36 additions & 55 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,33 @@ import {
useMemo,
useRef,
} from 'react'
import { EqualityChecker, State, StateSelector, UseBoundStore } from 'zustand'
import { EqualityChecker, State, StateSelector, StoreApi, useStore } from '.'
devanshj marked this conversation as resolved.
Show resolved Hide resolved

/**
* @deprecated Use `typeof MyContext.useStore` instead.
*/
export type UseContextStore<T extends State> = {
(): T
<U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U
type UseContextStore<S extends StoreApi<State>> = {
(): ExtractState<S>
<U>(
selector: StateSelector<ExtractState<S>, U>,
equalityFn?: EqualityChecker<U>
): U
}

function createContext<
TState extends State,
TUseBoundStore extends UseBoundStore<TState> = UseBoundStore<TState>
>() {
const ZustandContext = reactCreateContext<TUseBoundStore | undefined>(
undefined
)
type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type WithoutCallSignature<T> = { [K in keyof T]: T[K] }

function createContext<S extends StoreApi<State>>() {
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
const ZustandContext = reactCreateContext<S | undefined>(undefined)

const Provider = ({
initialStore,
createStore,
children,
}: {
/**
* @deprecated
*/
initialStore?: TUseBoundStore
createStore: () => TUseBoundStore
createStore: () => S
children: ReactNode
}) => {
const storeRef = useRef<TUseBoundStore>()
const storeRef = useRef<S>()

if (!storeRef.current) {
if (initialStore) {
console.warn(
'Provider initialStore is deprecated and will be removed in the next version.'
)
if (!createStore) {
createStore = () => initialStore
}
}
storeRef.current = createStore()
}

Expand All @@ -57,50 +43,45 @@ function createContext<
)
}

const useStore: UseContextStore<TState> = <StateSlice>(
selector?: StateSelector<TState, StateSlice>,
equalityFn = Object.is
const useBoundStore = (<StateSlice = ExtractState<S>>(
selector?: StateSelector<ExtractState<S>, StateSlice>,
equalityFn?: EqualityChecker<StateSlice>
) => {
// ZustandContext value is guaranteed to be stable.
const useProviderStore = useContext(ZustandContext)
if (!useProviderStore) {
const store = useContext(ZustandContext)
if (!store) {
throw new Error(
'Seems like you have not used zustand provider as an ancestor.'
)
}
return useProviderStore(
selector as StateSelector<TState, StateSlice>,
return useStore(
store,
selector as StateSelector<ExtractState<S>, StateSlice>,
equalityFn
)
}
}) as UseContextStore<S>

const useStoreApi = (): {
getState: TUseBoundStore['getState']
setState: TUseBoundStore['setState']
subscribe: TUseBoundStore['subscribe']
destroy: TUseBoundStore['destroy']
} => {
// ZustandContext value is guaranteed to be stable.
const useProviderStore = useContext(ZustandContext)
if (!useProviderStore) {
const useStoreApi = () => {
const store = useContext(ZustandContext)
if (!store) {
throw new Error(
'Seems like you have not used zustand provider as an ancestor.'
)
}
return useMemo(
() => ({
getState: useProviderStore.getState,
setState: useProviderStore.setState,
subscribe: useProviderStore.subscribe,
destroy: useProviderStore.destroy,
}),
[useProviderStore]
() =>
({
getState: store.getState,
setState: store.setState,
subscribe: store.subscribe,
destroy: store.destroy,
} as WithoutCallSignature<S>),
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
[store]
)
}

return {
Provider,
useStore,
useStore: useBoundStore,
useStoreApi,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './middleware/devtools'
export * from './middleware/subscribeWithSelector'
export * from './middleware/combine'
export * from './middleware/persist'
export * from './middleware/immer'
39 changes: 16 additions & 23 deletions src/middleware/combine.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { GetState, SetState, State, StoreApi } from '../vanilla'
import { NamedSet } from './devtools'
import { State, StateCreator, StoreMutatorIdentifier } from '../vanilla'

type Combine<T, U> = Omit<T, keyof U> & U
type Write<T, U> = Omit<T, keyof U> & U

export const combine =
<PrimaryState extends State, SecondaryState extends State>(
initialState: PrimaryState,
create: (
// Note: NamedSet added for convenience
set: SetState<PrimaryState> & NamedSet<PrimaryState>,
get: GetState<PrimaryState>,
api: StoreApi<PrimaryState>
) => SecondaryState
) =>
(
set: SetState<Combine<PrimaryState, SecondaryState>>,
get: GetState<Combine<PrimaryState, SecondaryState>>,
api: StoreApi<Combine<PrimaryState, SecondaryState>>
) =>
Object.assign(
{},
initialState,
create(set as any, get as any, api as any)
) as Combine<PrimaryState, SecondaryState>
type Combine = <
T extends State,
U extends State,
Mps extends [StoreMutatorIdentifier, unknown][] = [],
Mcs extends [StoreMutatorIdentifier, unknown][] = []
>(
initialState: T,
additionalStateCreator: StateCreator<T, Mps, Mcs, U>
) => StateCreator<Write<T, U>, Mps, Mcs>

export const combine: Combine =
(initialState, create) =>
(...a) =>
Object.assign({}, initialState, (create as any)(...a))
Loading