A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/peterlevel1/dva-reducer below:

peterlevel1/dva-reducer: dva reducer helper, redux reducer helper, extend the target prop inside the tree

  1. this is mainly used to be a helper for the dva's reducer
  2. also can be treated as a fast copy tool without deep walking into the json tree
  3. of course, redux reducer can also be applied with dvaReducer
  4. support default value as the placeholder
  5. call dvaReducer with no arg can be a useful method to cancel the ctrl+c|ctrl+v work
npm install dva-reducer --save
git clone https://github.com/peterlevel1/dva-reducer.git someDir/dva-reducer
cd someDir/dva-reducer
npm install
npm run test
to help your dva model reducer codes to be more readable
// file: some dir/app.js
var dvaReducer = require("dva-reducer")
var dva = require("dva")

dva.model({
  namespace: "caculator",
  state: {
    num: {
      num1: {
        num2: {
          num3: {
            num4: 0
          }
        }
      }
    }
  },
  reducers: {
    // the traditional reducer to update a value:
    // if the nested relation is a little deep,
    // we see the result of code is horrible,
    // and many bugs may show here, to avoid that you must be careful about the pyramid codes,
    // or you would meet some unknown bug
    "add": function(state, action) {
      return {
        ...state,
        num: {
          ...state.num,
          num1: {
            ...state.num.num1,
            num2: {
              ...state.num.num1.num2,
              num3: {
                ...state.num.num1.num2.num3,
                num4: state.num.num1.num2.num3.num4 - action.payload
              }
            }
          }
        }
      }
    },
    // we see the graceful codes with dvaReducer here
    "minus": dvaReducer({
      props: "num.num1.num2.num3.num4",
      update: function (state, payload, value) {
        return value - payload
      }
    })
  }
})

dva.route(...)
dva.start(...)

module.exports = dva

// file: some component or some route
dispatch({
  type: "caculator/add",
  payload: 10
})

dispatch({
  type: "caculator/minus",
  payload: 3
})

require("some dir/test.js")

// some dir/test.js
var dva = require("some dir/app.js")
var assert = require("assert")

var num = dva._getState().caculator.num
assert.equal(num, 7, "caculator.num: " + num)
// true
the returned function: reducer call dvaReducer.prod() if online

this would close the logs if sth wrong when dvaReducer valid your arg or prop value, so my advice is to see the test of dvaReducer first, and read the param doc up there, what's more, make some tests to keep everything in your control

reducer and shallow extend what the reducer return is an object with the given props route updated
var obj = {x: 1}
var state = {
  a: obj,
  b: {
    c: {
      d: 100
    }
  }
}

var action = {
  payload: 5
}

var reducer = dvaReducer({
  props: "b.c.d"
})

var nextState = reducer(state, action)

// nextState.a is equal to state.a, as the "a" prop is not within b.c.d
// but nextState.b or nextState.b.c or nextState.b.c.d is not equal to the state's according value

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4