1

像这样的js

import {createStore, applyMiddleware, combineReducers} from 'redux'
import {composeWithDevTools} from 'redux-devtools-extension'
import withRedux from 'next-redux-wrapper'
import nextReduxSaga from 'next-redux-saga'
import createSagaMiddleware from 'redux-saga'

import {initialState} from './initialState'
import globalSaga from '../sagas/global'

const sagaMiddleware = createSagaMiddleware()

export function configureStore (reducers,initialState) {
  console.log(initialState)

  let states = {}
  Object.keys(reducers).map((key) => {
    states[key] = initialState[key]
  })

  console.log(reducers)
  console.log(states)
  const store = createStore(
    combineReducers({reducers}),
    states,
    composeWithDevTools(applyMiddleware(sagaMiddleware))
  )
  store.sagaTask = sagaMiddleware.run(globalSaga)
  return store
}

export function withReduxSaga (BaseComponent, reducers) {
  return withRedux(configureStore(reducers,initialState))(nextReduxSaga(BaseComponent))
}

export default function configureWithReduxSaga(component,reducers){
  return withReduxSaga(component,reducers)
}

错误是

Store does not have a valid reducer. Make sure the argument passed to

makeStore is not a function

可能有什么问题,基本上我想编写一个函数,将具有特定 sagas 的reducersstore.global添加到 nextjs 组件中,而不需要太多代码,最好是这样的:

configureWithReduxSaga(
{reducer1, reducer2},
{saga1, saga2}
)

可能吗?我可以创建 root reducer 和 root saga 并将其传递给每个组件,这并不难编码,但它是否高效?

4

1 回答 1

1
combineReducers({reducers}),

很可能您有减速器字典,但将它们包装到冗余对象中。在这种情况下使用剩余扩展运算符combineReducers({...reducers})或简单对象传递combineReducers(reducers)。请参阅原始文档:https ://redux.js.org/docs/api/combineReducers.html#arguments

如果reducers是数组,你应该遍历它并给每个reducer自己的名字。

于 2017-11-20T08:01:54.850 回答