export
ing action types and action creators, that's enough! This "typed action creator"
factory will allow you to refer to the type from the action creator directly.
-
Create your action creator.
// actions.js import { typedActionCreatorFactory } from '@birdiecare/galette-core'; export const loadUser = typedActionCreatorFactory('LOAD_USER', username => ({ username }));
-
Uses your action type from the creator
// reducers.js import { loadUser } from './actions'; export const someReducer = (state, action) => { if (action.type === loadUser.type) { // do something... } }
-
Enjoy!
The 2nd parameter of the typedActionCreatorFactory
is the function that will
transform the arguments given to the action creator to the properties within the
action.
const loadTravels = typedActionCreatorFactory('LOAD_TRAVELS', (location, page = 1) => {
return {
location,
page,
}
});
const action = loadTravels('London', 2);
expect(action).toEqual({
type: 'LOAD_TRAVELS',
location: 'Rennes',
page: 2
})