Immer Support

Immer is a discover-it-then-cant-live-without-it library that lets you create immutable objects using an imperative-looking API, greatly enhancing readability. For example, the following:

  switch (action.type) {
    case 'USER_RENAME':
      return {
        ...state,
        user: {
          ...state.user,
          name: action.payload
        }
      }
  }

…using Immer becomes:

  switch (action.type) {
    case 'USER_RENAME':
      draft.user.name = action.payload
      return
  }

Redux Agent’s addTask and delTasks helpers have built-in support for Immer: pass them an Immer draft and you can use them imperatively. This:

import { addTask } from 'redux-agent'

const reducer = (state, action) => {
  switch(action.type) {
    case 'FETCH_USER':
      return addTask({ ...state, isLoading: true }, {
        type: 'http',
        method: 'get',
        url: 'https://jsonplaceholder.typicode.com/users/1'
        actions: { success: 'USER_SUCCESS' }
      })

…using Immer becomes:

import { produce } from 'immer'
import { addTask } from 'redux-agent'

const reducer = (state, action) => produce(state, (draft) => {
  switch(action.type) {
    case 'FETCH_USER':
      draft.isLoading = true
      addTask(draft, {
        type: 'http',
        method: 'get',
        url: 'https://jsonplaceholder.typicode.com/users/1'
        actions: { success: 'USER_SUCCESS' }
      })
      return

Read more about Immer in Introducing Immer: Immutability the easy way and on its project page.