You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- need to export both const type and action creator to use in multiple reducer files or redux-observable modules
317
-
- using `returntypeof()` helper function to infer Action types - (https://github.com/piotrwitek/react-redux-typescript#returntypeof-polyfill)
312
+
### KISS Solution
313
+
This solution is focused on KISS principle, without introducing any abstractions to be as close as possible to common Redux Pattern used in regular JavaScript solutions:
318
314
319
-
This case is focused on KISS, without introducing any abstractions to be as close as possible to common Redux Pattern used in JS.
315
+
- classic const based types
316
+
- very close to standard JS usage
317
+
- more boilerplate
318
+
- need to export action types and action creators to re-use in other layers like `redux-saga` or `redux-observable` modules
319
+
- using `returntypeof()` helper function to infer return type of Action Creators - (https://github.com/piotrwitek/react-redux-typescript#returntypeof-polyfill)
- `ActionCreator` helper factory function to create typed instances of actions - (https://github.com/piotrwitek/react-redux-typescript#helpers-v22)
367
-
- easier to use in multiple reducer files or `redux-observable` modules (action creators have type property and also create function, no extra type constant)
368
-
- very easy to get all of action types
363
+
### DRY Solution
364
+
This solution is using a simple helper factory function to automate creation of typed action creators. With little abstraction we can reduce boilerplate and code repetition, also it is easier to re-use action creators in other layers:
369
365
370
-
This case is using a helper factory function to create typed actions. With little abstraction we can significally reduce boilerplate and code repetition, also it is easier to re-use action creators in other reducer or redux-observable modules.
366
+
- using helper factory function to automate creation of typed action creators - recommended battle-tested `ActionCreator` from (https://github.com/piotrwitek/react-redux-typescript#helpers-v22)
367
+
- reduced boilerplate and code repetition
368
+
- easier to re-use in other layers like `redux-saga` or `redux-observable` modules (action creators have type property and also create function, no extra type constant)
@@ -403,9 +401,9 @@ export default function reducer(state: State = initialState, action: Action): St
403
401
- using Partial from (Mapped types)[https://www.typescriptlang.org/docs/handbook/advanced-types.html]
404
402
- to guard type of `partialState` and restrict superfluous or mismatched props when merging with State
405
403
406
-
### Switch Approach
404
+
### Switch Style
407
405
- using classic const based types
408
-
- good enough for single prop updates
406
+
- good for single prop updates or simple state objects
409
407
410
408
```ts
411
409
// State
@@ -434,9 +432,9 @@ export default function reducer(state: State = initialState, action: Action): St
434
432
```
435
433
436
434
### If Approach
437
-
- using `ActionCreator` helper types
438
-
- much better for multiple props update as it will ensure you that `PartialState` update is compatible with reducer state contract, this will guard you from errors
439
-
- if's "block scope" give you possibility to use local variables
435
+
- if's "block scope" give you possibility to use local variables for more complex state update logic
436
+
- better for more complex state objects - using partialState object spread for strongly typed multiple props update - it will ensure that action payload is compatible with reducer state contract - this will guard you from nasty bugs
437
+
- introducing optional static `type` property on `actionCreator` - advantage is to get rid of action types constants, as you can check type on action creator itself
440
438
441
439
```ts
442
440
// State
@@ -453,10 +451,10 @@ export const initialState: State = {
// - strongly typed application global state tree - `RootState`
540
+
// - should be imported in connected components providing type safety to Redux `connect` function
498
541
exporttypeRootState= {
499
542
currencyRates:CurrencyRatesState;
500
543
currencyConverter:CurrencyConverterState;
501
544
};
545
+
546
+
// - strongly typed application global action types - `Action`
547
+
// - should be imported in layers dealing with redux actions like: reducers, redux-sagas, redux-observables
548
+
exporttypeAction=
549
+
CurrencyRatesAction
550
+
|CurrencyConverterAction;
502
551
```
503
552
504
-
Use `RootState` in `combineReducers`function and as rehydrated State objecttype guard to obtain strongly typed Store instance
553
+
- creating store - use `RootState`(in `combineReducers`or when providing preloaded state object) to set-up *state object type guard* to leverage strongly typed Store instance
0 commit comments