什么是纯还原剂?

Adr*_*ius 2 javascript reactjs

我了解的是,在此视频中已经解释了“纯函数”的概念,这个问题是什么纯函数?

但是,我在此链接上阅读的助焊剂/氧化还原中遇到了“纯减压剂”一词

但是我不完全确定如何将这种“纯概念”应用于减速器,什么是纯减速器?

小智 5

这是我的理解,就redux而言,reducer是一个接受两个参数(状态,动作)的函数。

 1. state represents the current state of the application in store
 2. action represents the action that triggered
Run Code Online (Sandbox Code Playgroud)

Redux假定化简器确实接受当前状态并且不改变状态,但是根据操作类型返回新状态。如果它坚持并且不改变状态,那么它就是纯粹的还原剂。

/ **********************纯减速器的示例********************** ******* /

 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;
 }
Run Code Online (Sandbox Code Playgroud)

每当使用相同的参数集调用上述函数时,上述函数都没有副作用,它始终返回相同的输出。

/ *********************不纯的异径管示例*********************** **** /

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;
 }
Run Code Online (Sandbox Code Playgroud)