Skip to content

Commit

Permalink
update example more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
jin60641 committed Jan 7, 2024
1 parent 9c38e11 commit 60c62db
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,19 @@ counterReducer(0, increment()); // => 1
A default handler can be attached, which is invoked for all actions not associated with a handler.
```ts
const add = createAction('todos/ADD', length => length)<number>();
const reset = createAction('', () => 0)<number>();
const counterReducer = createReducer(0)
.handleAction(add, (state, action) => state + action.payload)
.defaultHandler((state, action) => state = action.payload);

const store = createStore(counterReducer);
store.dispatch(add(3));
store.dispatch(reset());
const increment = createAction('counter/INCREMENT')<number>();
const decrement = createAction('counter/DECREMENT')<number>();
const initialState = 0;
const counterReducer = createReducer(initialState)
.handleAction(increment, (state, action) => state + action.payload)
.handleAction(decrement, (state, action) => state - action.payload)
.defaultHandler(() => initialState);
const otherAction = createAction('others/ACTION', () => null)();

const store = createStore(counterReducer); // => 0
store.dispatch(increment(3)); // => 3
store.dispatch(decrement(2)); // => 2
store.dispatch(ohterAction()); // => 0
```
#### Alternative usage with regular switch reducer
Expand Down

0 comments on commit 60c62db

Please sign in to comment.