Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 16d6c66

Browse files
author
Charles Stover
committed
useDispatch with property behaves analogous to useReducer
1 parent c21933c commit 16d6c66

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ state. This allows you to write your reducers similar to React's `useReducer`.
325325
```JavaScript
326326
import React, { useDispatch } from 'reactn'; // <-- reactn
327327

328-
const incrementReducer = (global, dispatch, action) =>
329-
global.count + action.amount;
328+
const incrementReducer = (count, action) =>
329+
count + action.amount;
330330

331-
const decrementReducer = (global, dispatch, action) =>
332-
global.count - action.amount;
331+
const decrementReducer = (count, action) =>
332+
count - action.amount;
333333

334334
const MyComponent = () => {
335335
const increment = useDispatch(incrementReducer, 'count');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reactn",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"author": "Charles Stover <[email protected]>",
55
"description": "React, but with built-in global state management.",
66
"homepage": "https://github.com/CharlesStover/reactn#readme",

src/create-provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ export default function _createProvider<
9898
reducer: Reducer<G, R, A>,
9999
): Dispatcher<G, A>;
100100
public static useDispatch<A extends any[] = any[], P extends keyof G = keyof G>(
101-
reducer: PropertyReducer<G, R, A, P>,
101+
reducer: PropertyReducer<G, A, P>,
102102
property: P,
103103
): Dispatcher<G, A>;
104104
public static useDispatch<K extends keyof R = keyof R>(
105105
reducer: K,
106106
): Dispatcher<G, ExtractArguments<R[K]>>;
107107
public static useDispatch<K extends keyof R = keyof R, A extends any[] = any[], P extends keyof G = keyof G>(
108-
reducer?: K | Reducer<G, R, A> | PropertyReducer<G, R, A, P>,
108+
reducer?: K | Reducer<G, R, A> | PropertyReducer<G, A, P>,
109109
property?: P,
110110
): UseDispatch<G, R, K, A> {
111111

src/use-dispatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function _useDispatch<
4646
P extends keyof G = keyof G,
4747
>(
4848
overrideGlobalStateManager: GlobalStateManager<G, R> | null,
49-
reducer: PropertyReducer<G, R, A, P>,
49+
reducer: PropertyReducer<G, A, P>,
5050
property: P,
5151
): Dispatcher<G, A>;
5252

@@ -83,7 +83,7 @@ export default function _useDispatch<
8383
P extends keyof G = keyof G,
8484
>(
8585
overrideGlobalStateManager: GlobalStateManager<G, R> | null,
86-
reducer?: K | Reducer<G, R, A> | PropertyReducer<G, R, A, P>,
86+
reducer?: K | Reducer<G, R, A> | PropertyReducer<G, A, P>,
8787
property?: P,
8888
): UseDispatch<G, R, K, A> {
8989

@@ -111,11 +111,11 @@ export default function _useDispatch<
111111
if (isPropertyReducer(reducer, property)) {
112112
const newReducer: Reducer<G, R, A, Partial<G>> = (
113113
global: G,
114-
dispatch: Dispatchers<G, R>,
114+
_dispatch: Dispatchers<G, R>,
115115
...args: A
116116
): Partial<G> => {
117117
const newGlobalState: Partial<G> = Object.create(null);
118-
newGlobalState[property] = reducer(global, dispatch, ...args);
118+
newGlobalState[property] = reducer(global[property], ...args);
119119
return newGlobalState;
120120
};
121121
return globalStateManager.createDispatcher(newReducer);

src/utils/is-property-reducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default function isPropertyReducer<
99
A extends any[] = any[],
1010
P extends keyof G = keyof G,
1111
>(
12-
_reducer: Reducer<G, R, A> | PropertyReducer<G, R, A, P>,
12+
_reducer: Reducer<G, R, A> | PropertyReducer<G, A, P>,
1313
property?: keyof G,
14-
): _reducer is PropertyReducer<G, R, A, P> {
14+
): _reducer is PropertyReducer<G, A, P> {
1515
return typeof property !== 'undefined';
1616
};

tests/use-dispatch/function-string.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import GlobalStateManager from '../../src/global-state-manager';
22
import useDispatch from '../../src/use-dispatch';
33
import REACT_HOOKS_ERROR from '../../src/utils/react-hooks-error';
44
import Dispatcher from '../../types/dispatcher';
5-
import Dispatchers from '../../types/dispatchers';
65
import { PropertyReducer } from '../../types/reducer';
76
import HookTest from '../utils/hook-test';
87
import { G, INITIAL_STATE } from '../utils/initial';
@@ -17,18 +16,18 @@ type P = [ ZReducer, keyof G ];
1716

1817
type V = Dispatcher<G, A>;
1918

20-
type ZReducer = PropertyReducer<G, {}, string[], 'z'>;
19+
type ZReducer = PropertyReducer<G, string[], 'z'>;
2120

2221

2322

2423
const ARGS: string[] = [ 'te', 'st' ];
2524

2625
const REDUCER: ZReducer =
27-
(global: G, _dispatch: Dispatchers<G, {}>, ...args: string[]): G['z'] =>
28-
global.z + args.join('');
26+
(z: G['z'], ...args: string[]): G['z'] =>
27+
z + args.join('');
2928

3029
const STATE_CHANGE: Partial<G> = {
31-
z: REDUCER(INITIAL_STATE, {}, ...ARGS),
30+
z: REDUCER(INITIAL_STATE.z, ...ARGS),
3231
};
3332

3433
const NEW_STATE: G = {

types/provider.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Callback from './callback';
33
import Dispatcher, { ExtractArguments } from './dispatcher';
44
import Dispatchers from './dispatchers';
55
import NewGlobalState from './new-global-state';
6-
import Reducer, { AdditionalReducers } from './reducer';
6+
import Reducer, { AdditionalReducers, PropertyReducer } from './reducer';
77
import { GlobalTuple, StateTuple } from './use-global';
88
import WithGlobal, { Getter, Setter } from './with-global';
99

@@ -17,40 +17,53 @@ export default interface ReactNProvider<
1717
G extends {} = State,
1818
R extends {} = Reducers,
1919
> {
20+
2021
addCallback(callback: Callback<G>): BooleanFunction;
22+
2123
addReducer<A extends any[] = any[]>(
2224
name: string,
2325
reducer: Reducer<G, R, A>,
2426
): BooleanFunction;
27+
2528
addReducers(reducers: AdditionalReducers<G, R>): BooleanFunction;
2629
dispatch: Dispatchers<G, R>;
30+
2731
getDispatch(): Dispatchers<G, R>;
32+
2833
getGlobal(): G;
2934
global: G;
35+
3036
removeCallback(callback: Callback<G>): boolean;
37+
3138
reset(): void;
39+
3240
setGlobal(
3341
newGlobalState: NewGlobalState<G>,
3442
callback?: Callback<G>,
3543
): Promise<G>;
44+
3645
useDispatch(): Dispatchers<G, R>;
3746
useDispatch<A extends any[] = any[]>(
3847
reducer: Reducer<G, R, A>,
3948
): Dispatcher<G, A>;
4049
useDispatch<A extends any[] = any[], P extends keyof G = keyof G>(
41-
reducer: Reducer<G, R, A, G[P]>,
50+
reducer: PropertyReducer<G, A, P>,
4251
property: P,
4352
): Dispatcher<G, A>;
4453
useDispatch<K extends keyof R = keyof R>(
4554
reducer: K,
4655
): Dispatcher<G, ExtractArguments<R[K]>>;
56+
4757
useGlobal(): GlobalTuple<G>;
4858
useGlobal<Property extends keyof G>(
4959
property: Property,
5060
): StateTuple<G, Property>;
61+
5162
withGlobal<HP, LP>(
5263
getter?: Getter<G, R, HP, LP>,
5364
setter?: Setter<G, R, HP, LP>,
5465
): WithGlobal<HP, LP>;
66+
5567
new (props: {}, context?: any): React.Component<{}, {}>;
68+
5669
}

types/reducer.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ export interface AdditionalReducers<
1111

1212
export interface PropertyReducer<
1313
G extends {} = State,
14-
R extends {} = Reducers,
1514
A extends any[] = any[],
1615
P extends keyof G = keyof G,
1716
> extends CallableFunction {
18-
(global: G, dispatch: Dispatchers<G, R>, ...args: A): G[P];
17+
(value: G[P], ...args: A): G[P];
1918
}
2019

2120

0 commit comments

Comments
 (0)