This code violates DRY by duplicating the action creator pattern for different entities. A better approach would be to create a factory function:
const createAsyncActions = (entity) => {
const uppercase = entity.toUpperCase();
return {
start: () => ({
type: `FETCH_${uppercase}_START`
}),
success: (data) => ({
type: `FETCH_${uppercase}_SUCCESS`,
payload: data
}),
error: (error) => ({
type: `FETCH_${uppercase}_ERROR`,
payload: error
})
};
};
const userActions = createAsyncActions('user');
const productActions = createAsyncActions('product');
// Usage:
dispatch(userActions.start());
dispatch(productActions.success(data));
This approach:
1. Eliminates action creator duplication
2. Makes adding new entity actions easier
3. Ensures consistency across action creators
4. Reduces the chance of typos in action types