Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React with Redux项目中为API服务调用实现动态reducer creator?_Javascript_Reactjs_Redux_React Redux_Reducers - Fatal编程技术网

Javascript 如何在React with Redux项目中为API服务调用实现动态reducer creator?

Javascript 如何在React with Redux项目中为API服务调用实现动态reducer creator?,javascript,reactjs,redux,react-redux,reducers,Javascript,Reactjs,Redux,React Redux,Reducers,因为我可能有很多API服务要调用,所以我必须为这些服务编写尽可能多的reducer,有没有办法像下面这样动态地实现reducer创建者 const PENDING = 'PENDING' const REJECTED = 'REJECTED' const FULFILLED = 'FULFILLED' const COMPANIES = 'COMPANIES' let createReducer = (name) => (state, action) => { switch (

因为我可能有很多API服务要调用,所以我必须为这些服务编写尽可能多的reducer,有没有办法像下面这样动态地实现reducer创建者

const PENDING = 'PENDING'
const REJECTED = 'REJECTED'
const FULFILLED = 'FULFILLED'
const COMPANIES = 'COMPANIES'

let createReducer = (name) => (state, action) => {
  switch (action.type) {
    case name + '_' + PENDING:
      return {...state, 
        isLoading: false
      }
    case name + '_' + FULFILLED:
      return {...state, 
        companies: action.payload,
        isLoading: false
      }
    case name + '_' + REJECTED:
      return {...state, 
        isLoading: false,
        err: action.payload
      }
    default:
      return state
  }
}

let comapnyReducer = createReducer(COMPANIES)
这可以等效于以下显式实现:

const comapnyReducer = (state={isLoading: false}, action) => {
switch (action.type) {
  case 'COMPANIES_PENDING':
    return {...state, 
      isLoading: false
    }
  case 'COMPANIES_FULFILLED':
    return {...state, 
      companies: action.payload,
      isLoading: false
    }
  case 'COMPANIES_REJECTED':
    return {...state, 
      isLoading: false,
      err: action.payload
    }
  default:
    return state
  }
}

这个例子的目的是说明它可能是怎样的。您的实现可能在细节上有所不同

行动示例

{type:'COMPANIES_FULFILLED',name:'COMPANIES',payload:[1,2,3,4]}
结果

    "companies": {
        "items": [
            1,
            2,
            3,
            4
        ],
        "isLoading": false
    },
    "messages": {
        "items": [
            1,
            2,
            3,
            4
        ],
        "isLoading": false
    }

在我看到的输出上,state应该是什么样子的?实际上,您在操作中添加了一个属性'name',以便让reducer计算操作类型的常量值。在我的例子中,我应用了redux promise中间件,因此action对象中的负载是promise,并且action类型将在不同的API调用阶段动态更改。但是我不知道为什么reducer不能意识到action对象的'name'属性。还有一件更重要的事情是,即使我能够成功地将操作分派给reducer,事情也可能会出错,因为如果组件的任何部分遇到错误,减速器无法正确生成,这意味着整个状态机将卡在某个地方。因此,我开始重新思考这种生成动态减速器的方法是否是一种反模式?或者有更好的方法?上面的调度器,
dispatch({type:“companys”,payload:fetch)(http://mydomain/companies/,{credentials:'include'})。然后(response=>response.json()),名称:“companys”})
    "companies": {
        "items": [
            1,
            2,
            3,
            4
        ],
        "isLoading": false
    },
    "messages": {
        "items": [
            1,
            2,
            3,
            4
        ],
        "isLoading": false
    }