Typesafe utilities for "action-creators" in Redux / Flux Architecture
createReducer
a typesafe reducer factory using object map and chain API #106From v4.x.x
all action creators will use undefined
instead of void
as a generic type parameter to make the action-creator function require NO parameters.
Background discussion: https://github.com/piotrwitek/typesafe-actions/issues/124#issuecomment-479446603
const increment = createStandardAction('INCREMENT')<undefined>();
increment(); // <= no parameters required
const fetchUsers = createAsyncAction(
'FETCH_USERS_REQUEST',
'FETCH_USERS_SUCCESS',
'FETCH_USERS_FAILURE'
)<undefined, User[], Error>();
fetchUsers.request(); // <= no parameters required
action
and createAction
have new 3rd parameter which is error property on action object making it fully FSA compliant.
import { action, createAction } from 'typesafe-actions';
export const todosError = (message: string) => action('todos/ERROR', message, undefined, true);
// todosError: (message: string) => { type: "todos/ADD"; payload: string; error: boolean; }
export const todosError = createAction('todos/ERROR', action => {
// Note: "action" callback does not need "type" parameter
return (message: string) => action(message, undefined, true);
});
// todosError: (message: string) => { type: "todos/ADD"; payload: string; error: boolean; }
Fixed #117 Fixed #118
Create an enhanced action-creator with unlimited number of arguments and custom properties on action object.
(id, firstName, lastName)
.({ type, customProp1, customProp2, ...customPropN })
createCustomAction(type, type => {
return (namedArg1, namedArg2, ...namedArgN) => ({ type, customProp1, customProp2, ...customPropN })
})
Examples: > Advanced Usage Examples
import { createCustomAction } from 'typesafe-actions';
const add = createCustomAction('CUSTOM', type => {
return (first: number, second: number) => ({ type, customProp1: first, customProp2: second });
});
add(1) // { type: "CUSTOM"; customProp1: number; customProp2: number; }