use-typed-reducer 3.2.0 → 3.2.1

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
@@ -24,7 +24,6 @@ With useTypedReducer, you can use your function the way you prefer, it will infe
24
24
 
25
25
  ### useReducer (default import) or `useTypedReducer`
26
26
 
27
-
28
27
  `useTypedReducer` receive the initialState and dictionary/object with all reducers and return tuple with state and dispatch. Dispatch has the same key and functions of given dictionary in `useTypedReducer`, but return a new function to update state. This void `dispatch({ type: "ACTION" })`
29
28
 
30
29
  ```tsx
@@ -72,48 +71,58 @@ const Component = () => {
72
71
  The same of useTypedReducer, but receive a `getProps` as second argument in the second function.
73
72
 
74
73
 
75
- ```typescript
74
+ ```typescript jsx
76
75
  import { useReducerWithProps, UseReducer } from "./index";
77
76
 
78
- const initialState = {
79
- numbers: 0,
80
- something: ""
81
- }
77
+ const initialState = { numbers: 0, something: "" };
82
78
 
83
79
  type State = typeof initialState;
84
80
 
85
81
  type Reducers = {
86
- reset: UseReducer.Reducer<State, () => any>;
87
- onChange: UseReducer.Reducer<State, (e: React.ChangeEvent<HTMLInputElement>) => any>;
88
- increment: UseReducer.Reducer<State, () => any>;
82
+ reset: UseReducer.Reducer<State, () => any>;
83
+ onChange: UseReducer.Reducer<State, (e: React.ChangeEvent<HTMLInputElement>) => any>;
84
+ increment: UseReducer.Reducer<State, () => any>;
89
85
  };
90
86
 
91
87
  type Props = {
92
- list: number[];
88
+ list: number[];
93
89
  }
94
90
 
95
91
  const reducers: Reducers = {
96
- increment: () => (state) => ({ ...state, numbers: state.numbers + 1 }),
97
- onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
98
- const value = e.target.valueAsNumber;
99
- return (state, props) => {
100
- const find = props.list.find(x => x === value);
101
- return find === undefined ? ({ ...state, numbers: value }): ({ ...state, numbers: find * 2 });
102
- }
103
- },
104
- reset: () => (state) => ({ ...state, numbers: 0 })
92
+ increment: () => (state) => ({ ...state, numbers: state.numbers + 1 }),
93
+ onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
94
+ const value = e.target.valueAsNumber;
95
+ return (state, props) => {
96
+ const find = props.list.find(x => x === value);
97
+ return find === undefined ? ({ ...state, numbers: value }) : ({ ...state, numbers: find * 2 });
98
+ };
99
+ },
100
+ reset: () => (state) => ({ ...state, numbers: 0 })
105
101
  };
106
102
 
107
103
 
108
104
  const Component = (props: Props) => {
109
- const [state, dispatch] = useReducerWithProps(initialState, props, reducers)
105
+ const [state, dispatch] = useReducerWithProps(initialState, props, reducers);
106
+
107
+ return (
108
+ <Fragment>
109
+ <button onClick={dispatch.increment}>+Increment</button>
110
+ <input onChange={dispatch.onChange} value={state.numbers} />
111
+ <button onClick={dispatch.reset}>Reset</button>
112
+ </Fragment>
113
+ );
114
+ };
115
+ ```
110
116
 
111
- return (
112
- <>
113
- <button onClick={dispatch.increment}>+Increment</button>
114
- <input onChange={dispatch.onChange} value={state.numbers} />
115
- <button onClick={dispatch.reset}>Reset</button>
116
- </>
117
- )
118
- }
117
+ ## useReducer
118
+
119
+ This is the new way to control your state, but with the old way of use-typed-reducer. Now you have a function to getState and a function to get the props. With this you can avoid to take a function that return a function to update state.
120
+
121
+ ```typescript
122
+ export const useMath = () => {
123
+ return useReducer({ count: 0 }, (getState) => ({
124
+ sum: (n: number) => ({ count: n + getState().count }),
125
+ diff: (n: number) => ({ count: n - getState().count })
126
+ }));
127
+ };
119
128
  ```
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ declare type VoidableFn<Fn extends (...any: any[]) => any> = ReturnType<Fn> extends Promise<any> ? (...a: Parameters<Fn>) => Promise<void> : (...a: Parameters<Fn>) => void;
1
2
  declare type Action<State> = (...args: any) => Promise<(state: State) => State> | ((state: State) => State);
2
3
  declare type Actions<Actions, State> = {
3
4
  [key in keyof Actions]: Action<State>;
@@ -16,7 +17,10 @@ declare type DefaultReducer<S extends {}> = (state: S) => S;
16
17
  declare type WithProps<S extends {}, P> = (state: S, props: P) => S;
17
18
  export declare type Reducer<S extends {}, A extends (...args: any) => DefaultReducer<S>> = (...params: Parameters<A>) => DefaultReducer<S>;
18
19
  export declare type ReducerWithProps<S extends {}, P extends {}, A extends (...args: any) => WithProps<S, P>> = (...params: Parameters<A>) => WithProps<S, P>;
19
- export declare const useTypedReducer: <State extends {}, Reducers extends Dispatch<State, Reducers>>(initialState: State, reducers: Reducers) => [state: State, dispatch: Reducers];
20
+ declare type RemoveReduceReturns<State extends {}, Reducers extends Dispatch<State, Reducers>> = {
21
+ [R in keyof Reducers]: VoidableFn<Reducers[R]>;
22
+ };
23
+ export declare const useTypedReducer: <State extends {}, Reducers extends Dispatch<State, Reducers>>(initialState: State, reducers: Reducers) => [state: State, dispatch: RemoveReduceReturns<State, Reducers>];
20
24
  export declare const useReducerWithProps: <State extends {}, Props extends {}, Reducers extends DispatchProps<State, Props, Reducers>>(initialState: State, props: Props, reducers: Reducers) => [state: State, dispatch: Reducers];
21
25
  declare type FnMap<State> = {
22
26
  [k: string]: (...args: any[]) => Partial<State> | Promise<Partial<State>>;
@@ -24,5 +28,8 @@ declare type FnMap<State> = {
24
28
  declare type MappedReducers<State extends {}, Fns extends FnMap<State>> = {
25
29
  [F in keyof Fns]: (...args: Parameters<Fns[F]>) => State | Promise<State>;
26
30
  };
27
- export declare const useReducer: <State extends {}, Reducers extends (getState: () => State, getProps: () => Props) => MappedReducers<State, FnMap<State>>, Props extends {}>(initialState: State, reducer: Reducers, props?: Props | undefined) => [state: State, dispatchers: MappedReducers<State, ReturnType<Reducers>>];
31
+ declare type MapReducerReturn<State extends {}, Fns extends FnMap<State>> = {
32
+ [F in keyof Fns]: VoidableFn<Fns[F]>;
33
+ };
34
+ export declare const useReducer: <State extends {}, Reducers extends (getState: () => State, getProps: () => Props) => MappedReducers<State, FnMap<State>>, Props extends {}>(initialState: State, reducer: Reducers, props?: Props | undefined) => [state: State, dispatchers: MapReducerReturn<State, ReturnType<Reducers>>];
28
35
  export default useTypedReducer;
package/dist/index.es.js CHANGED
@@ -167,18 +167,10 @@ var useReducer = function (initialState, reducer, props) {
167
167
  for (var _i = 0; _i < arguments.length; _i++) {
168
168
  params[_i] = arguments[_i];
169
169
  }
170
- return __awaiter(void 0, void 0, void 0, function () {
171
- var newState;
172
- return __generator(this, function (_a) {
173
- switch (_a.label) {
174
- case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
175
- case 1:
176
- newState = _a.sent();
177
- setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
178
- return [2 /*return*/];
179
- }
180
- });
181
- });
170
+ var newState = dispatch.apply(void 0, __spread(params));
171
+ return newState instanceof Promise
172
+ ? newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
173
+ : setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
182
174
  }, _b)));
183
175
  }, reducers);
184
176
  }, []);
package/dist/index.js CHANGED
@@ -171,18 +171,10 @@ var useReducer = function (initialState, reducer, props) {
171
171
  for (var _i = 0; _i < arguments.length; _i++) {
172
172
  params[_i] = arguments[_i];
173
173
  }
174
- return __awaiter(void 0, void 0, void 0, function () {
175
- var newState;
176
- return __generator(this, function (_a) {
177
- switch (_a.label) {
178
- case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
179
- case 1:
180
- newState = _a.sent();
181
- setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
182
- return [2 /*return*/];
183
- }
184
- });
185
- });
174
+ var newState = dispatch.apply(void 0, __spread(params));
175
+ return newState instanceof Promise
176
+ ? newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
177
+ : setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
186
178
  }, _b)));
187
179
  }, reducers);
188
180
  }, []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-typed-reducer",
3
- "version": "3.2.0",
3
+ "version": "3.2.1",
4
4
  "description": "use-reducer React hook alternative",
5
5
  "author": "g4rcez",
6
6
  "license": "MIT",
@@ -17,7 +17,8 @@
17
17
  },
18
18
  "scripts": {
19
19
  "build": "rollup -c",
20
- "start": "rollup -c -w"
20
+ "start": "rollup -c -w",
21
+ "test": "vitest run"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "react": ">= 16.8.3"
@@ -26,12 +27,18 @@
26
27
  "@rollup/plugin-node-resolve": "9.0.0",
27
28
  "@rollup/plugin-strip": "2.0.0",
28
29
  "@rollup/plugin-url": "5.0.1",
30
+ "@testing-library/jest-dom": "5.17.0",
31
+ "@testing-library/react": "14.0.0",
29
32
  "@types/react": "16.9.19",
30
33
  "@types/react-dom": "16.9.4",
34
+ "@vitejs/plugin-react": "4.0.3",
31
35
  "babel-core": "6.26.3",
32
36
  "babel-runtime": "6.26.0",
33
37
  "cross-env": "7.0.0",
34
- "prettier": "2.8.7",
38
+ "jsdom": "22.1.0",
39
+ "prettier": "3.0.0",
40
+ "react": "18.2.0",
41
+ "react-dom": "18.2.0",
35
42
  "react-scripts-ts": "3.1.0",
36
43
  "rollup": "2.26.11",
37
44
  "rollup-plugin-alias": "2.2.0",
@@ -46,7 +53,9 @@
46
53
  "rollup-plugin-url": "3.0.1",
47
54
  "rollup-plugin-visualizer": "4.1.1",
48
55
  "tslib": "2.0.1",
49
- "typescript": "4.0.2"
56
+ "typescript": "4.0.2",
57
+ "vite": "4.4.4",
58
+ "vitest": "0.33.0"
50
59
  },
51
60
  "files": [
52
61
  "dist"