extra-react-store 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,6 +8,7 @@ yarn add extra-react-store
8
8
 
9
9
  ## Usage
10
10
  ```tsx
11
+ import { Draft } from 'immer'
11
12
  import { useMemo } from 'react'
12
13
  import { createStoreContext, Store, useSelector, useUpdater } from 'extra-react-store'
13
14
 
@@ -40,12 +41,14 @@ function Controller() {
40
41
  const update = useUpdater<IState>(Context)
41
42
 
42
43
  return (
43
- <button onClick={() => update(increment)}>Increment</button>
44
+ <button onClick={() => update(increment(1))}>Increment</button>
44
45
  )
45
46
  }
46
47
 
47
- function increment(state: IState): void {
48
- state.count++
48
+ function increment(value: number): (state: Draft<IState>) => void {
49
+ return state => {
50
+ state.count += value
51
+ }
49
52
  }
50
53
  ```
51
54
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-partial-updater.js","sourceRoot":"","sources":["../src/use-partial-updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAC/C,OAAO,EAAS,OAAO,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,EAAE,EAAE,EAAE,MAAM,qBAAqB,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAGxC,MAAM,UAAU,iBAAiB,CAC/B,OAA4B,EAC5B,mBAAmD,EACnD,iBAAsE;IAEtE,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,OAAO,GAA0B,WAAW,CAAC,mBAAmB,CAAC,EAAE;QACvE,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;QAEjC,MAAM,eAAe,GAAiB,EAAE,CAAC,GAAG,EAAE;YAC5C,IAAI,UAAU,CAAC,mBAAmB,CAAC,EAAE;gBACnC,MAAM,EAAE,GAAG,mBAAmB,CAA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AC;gBACN,MAAM,eAAe,GAAG,mBAAmB,CAAA;gBAC3C,OAAO,eAAe,CAAA;YACxB,CAAC;QACH,CAAC,CAAC,CAAA;QAEF,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAA;IAC9D,CAAC,EAAE,CAAC,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,CAAC,CAAC,CAAA;IAEnD,OAAO,OAAO,CAAA;AAChB,CAAC"}
@@ -1,16 +1,16 @@
1
- import { useState, useContext, useEffect } from 'react';
1
+ import { useState, useContext, useLayoutEffect } from 'react';
2
2
  import { isReferenceEqual } from 'extra-utils';
3
3
  export function useSelector(context, selector, isEqual = isReferenceEqual) {
4
4
  const store = useContext(context);
5
5
  const [[value], setBox] = useState(() => [selector(store.getState())]);
6
- useEffect(() => {
6
+ useLayoutEffect(() => {
7
7
  return store.subscribe(state => {
8
8
  const newValue = selector(state);
9
9
  if (!isEqual(value, newValue)) {
10
10
  setBox([newValue]);
11
11
  }
12
12
  });
13
- }, [store, selector, value, setBox]);
13
+ }, [store, selector, isEqual, value, setBox]);
14
14
  return value;
15
15
  }
16
16
  //# sourceMappingURL=use-selector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC,EACjC,UAA2C,gBAAgB;IAE3D,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IACjC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CA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AAK,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IAE7C,OAAO,KAAK,CAAA;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-store",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -37,8 +37,8 @@
37
37
  "@commitlint/config-conventional": "^17.6.7",
38
38
  "@testing-library/react": "^14.0.0",
39
39
  "@types/react": "^18.2.15",
40
- "@typescript-eslint/eslint-plugin": "^6.1.0",
41
- "@typescript-eslint/parser": "^6.1.0",
40
+ "@typescript-eslint/eslint-plugin": "^6.14.0",
41
+ "@typescript-eslint/parser": "^6.14.0",
42
42
  "@vitejs/plugin-react": "^4.0.3",
43
43
  "eslint": "^8.45.0",
44
44
  "eslint-plugin-react": "^7.33.0",
@@ -52,9 +52,9 @@
52
52
  "react-dom": "^18.2.0",
53
53
  "rimraf": "^5.0.1",
54
54
  "standard-version": "^9.3.2",
55
- "ts-patch": "^3.0.2",
56
- "tslib": "^2.6.0",
57
- "typescript": "5.1.6",
55
+ "ts-patch": "^3.1.1",
56
+ "tslib": "^2.6.2",
57
+ "typescript": "5.3.3",
58
58
  "typescript-transform-paths": "^3.4.6",
59
59
  "vite": "^4.4.5",
60
60
  "vite-tsconfig-paths": "^4.2.0",
@@ -1,4 +1,4 @@
1
- import { useState, useContext, useEffect } from 'react'
1
+ import { useState, useContext, useLayoutEffect } from 'react'
2
2
  import { StoreContext } from './types.js'
3
3
  import { isReferenceEqual } from 'extra-utils'
4
4
 
@@ -10,7 +10,8 @@ export function useSelector<State, Value>(
10
10
  const store = useContext(context)
11
11
  const [[value], setBox] = useState(() => [selector(store.getState())])
12
12
 
13
- useEffect(() => {
13
+ // `useEffect` will be called too late, resulting in not subscribing in time.
14
+ useLayoutEffect(() => {
14
15
  return store.subscribe(state => {
15
16
  const newValue = selector(state)
16
17
 
@@ -18,7 +19,7 @@ export function useSelector<State, Value>(
18
19
  setBox([newValue])
19
20
  }
20
21
  })
21
- }, [store, selector, value, setBox])
22
+ }, [store, selector, isEqual, value, setBox])
22
23
 
23
24
  return value
24
25
  }