Sometimes we write too much repetition in our action creators.
Like so:
export const fetchProducts = () => ({
type: FETCH_PRODUCTS
})
export const fetchProductsSuccessful = payload => ({
type: FETCH_PRODUCTS_SUCCESSFUL,
payload
})
export const fetchProductsFailure = error => ({
type: FETCH_PRODUCTS_FAILURE,
payload: error
})
export const fetchUsers = () => ({
type: FETCH_USERS
})
export const fetchUsersSuccessful = payload => ({
type: FETCH_USERS_SUCCESSFUL,
payload
})
export const fetchUsersFailure = error => ({
type: FETCH_USERS_FAILURE,
payload: error
})
This library tries to remove this repetion encapsulating common actions in an action generator.
$ npm install --save reshort
import reshort from "reshort";
const productsActions = reshort("Products");
productsActions("request")
// {
// type: "GET_PRODUCTS"
// }
productsActions("success", {test: 123})
// {
// type: "GET_PRODUCTS_SUCCESSFUL",
// payload: { test: 123 }
// }
productsActions("fail", {test: "error"})
// {
// type: "GET_PRODUCTS_FAILURE",
// payload: { test: "error" }
// }
MIT © Robson Porto