0.0.2 • Published 7 years ago

redux-ar v0.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

redux-ar

It just makes actions and reducers. Nothing fancy.

Why make another library when tree shaking exists?

Tree shaking is cool. If you're only concerned with file size, go ahead and run Webpack 2 on redux-actions. This library covers two far more important issues:

  1. A function that creates reducers should be called createReducer

  2. The initial state for a function that creates reducers should be specified as the first parameter, just like how the reducer would look if you wrote it manually.

These are petty issues that would never survive as a PR on any of those more popular libraries. If you believe that a function that creates action creators should be called createActionCreator, feel free to fork this library and rename that function. I would like to suggest the project name redux-acr.

Usage

import {createAction, createReducer} from 'redux-ar'

const increment = createAction('INCREMENT')
// increment(2) == {type: 'INCREMENT', payload: 2}

const login = createAction('LOGIN', (username, password) => ({username, password}))
// login('Alice', 'hunter2') == {type: 'LOGIN', payload: {username: 'Alice', password: 'hunter2'}}

const fetch = createAction('FETCH', () => (dispatch, getState) => {
  // something with dispatch and getState
})
// This example uses redux-thunk. The point is, you can use middleware.

const reducer = createReducer({
  counter: 0,
  credentials: null
}, {
 [increment]: (state, action) => ({...state, counter: counter + action.payload}),
 [login] (state, action) {
   return {
     ...state,
     credentials: action.payload
   }
 }
})
// There's quite a few ways you can write this. The action type just needs to be a property of 
// the object passed as the second parameter.

TODO

  • Write unit tests
  • Add JSDoc comments