最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a pure reducer? - Stack Overflow

programmeradmin0浏览0评论

What I understand is that there is a concept of "Pure functions" which I got explained in this video and this question What is pure functions?

However I have encountered the term "Pure reducers" in the context of flux/redux I read on this link

But I'm not entirely sure how to apply this "pure concept" to reducers, what is a pure reducer?

What I understand is that there is a concept of "Pure functions" which I got explained in this video and this question What is pure functions?

However I have encountered the term "Pure reducers" in the context of flux/redux I read on this link

But I'm not entirely sure how to apply this "pure concept" to reducers, what is a pure reducer?

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked May 26, 2016 at 8:13 Adrian ForsiusAdrian Forsius 1,4382 gold badges19 silver badges29 bronze badges 2
  • 2 a reducer is a function. a specific kind of one, but just a function. same thing. A rose by any other name... – dandavis Commented May 26, 2016 at 8:15
  • Reducer is a function, so that's just a more specific name. – Omri Aharon Commented May 26, 2016 at 8:16
Add a ment  | 

3 Answers 3

Reset to default 10

Here is my understanding, in terms of redux a reducer is a function which accepts two arguments (state, action).

 1. state represents the current state of the application in store
 2. action represents the action that triggered

Redux assumes that the reducers does accept the current state and don't mutate the state but returns the new state, depending on the action type. If it adheres and don't mutate the state then it is a pure reducer.

/********************** example of a pure reducer *****************************/

 var initialState = {counter:0};
 function counterReducer(state = initialState, action){
     if (action.type === 'INCREMENT'){
         // returns a new state incrementing a counter
         return {counter:state.counter + 1};
     }
     else if (action.type === 'DECREMENT'){
         // return a new state decrementing a counter
        return {counter:state.counter - 1};
     }

     //  returns the state as is
     return state;
 }

The above function has no side-effects whenever it is invoked with the same set of arguments it always returns the same output.

/********************* example of an impure reducer ***************************/

var initialState = {counter:0};
 function counterReducer(state = initialState, action){
     if (action.type === 'INCREMENT'){
         // modifies state by mutating or incrementing the counter in state
         state.counter++;
     }
     else if (action.type === 'DECREMENT'){
         // modifies state by mutating or decrementing the counter in state
        state.counter--;
     }

     //  returns the state
     return state;
 }

Another form of impure reducer would be one that doesn't return the same value given the same arguments. So using Dhananjaya Kuppu's example:

return {counter: state.counter + Math.floor(Math.random())}

a reducer is simply a function that is passed as an argument to the reduce function of an array. For example:

const sumReducer = (acc, x) => acc + x
const multReducer = (acc, x) => acc * x

const sumResult = [1,2,3,4,5].reduce (sumReducer, 0)
const multResult = [1,2,3,4,5].reduce (multReducer, 1)

That's basically a reducer.

发布评论

评论列表(0)

  1. 暂无评论