25.0.0 • Published 4 years ago

@mirango/re-ts v25.0.0

Weekly downloads
91
License
MIT
Repository
-
Last release
4 years ago

re-ts

A redux helper library for TypeScript. Historically using TypeScript with redux resulted in a lot of boilerplate code. With the most recent additions to the language it is now possible to write convenient helper functions that generate type-safe action creators, action types and reducers.

Install

yarn add --dev @mirango/re-ts

Example

import { CreateActionType, createActions, makeReducer } from ".";

// Use the createActions helper function to define your actions and action types at the same time
export const [actions, actionTypes] = createActions(action => ({
  increment: action("counter/increment", (value: number) => ({ value })),
  decrement: action("counter/decrement", (value: number) => ({ value })),
  reset: action("counter/reset")
}));

// You can use the CreateActionType helper to define your action type
type TodoAction = CreateActionType<typeof actions>;

// Defining the reducer
const initialState = {
  counter: 0
};

// Create your reducer by specifying:
// - the action type(s) it can handle as the type parameter
// - the initial state
// - and the handler functions for the handled action types
export const reducer = makeReducer<TodoAction>()(initialState, {
  [actionTypes.increment]: (state, { value }) => {
    state.counter += value;
  },
  [actionTypes.decrement]: (state, { value }) => {
    state.counter -= value;
  },
  [actionTypes.reset]: state => {
    state.counter = 0;
  }
});
25.0.0

4 years ago

23.0.0

4 years ago

24.0.0

4 years ago

22.0.0

5 years ago

21.0.0

5 years ago

20.0.0

5 years ago

19.0.0

5 years ago

18.0.0

5 years ago

17.0.0

5 years ago

16.1.0

6 years ago

16.0.0

6 years ago

14.0.0

6 years ago

13.0.0

6 years ago

12.2.0

6 years ago

12.1.0

6 years ago

12.0.0

6 years ago

11.4.0

6 years ago

11.3.0

6 years ago

11.2.0

6 years ago

11.1.0

6 years ago

11.0.0

6 years ago

10.0.0

6 years ago

9.0.0

6 years ago

8.0.0

6 years ago

7.0.0

6 years ago

6.0.0

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago