使用redux redux-saga的Nextjs初始化商店

时间:2017-11-08 21:41:10

标签: javascript redux redux-saga nextjs

喜欢这个

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

可能有什么问题,基本上我想编写将 reducers store.global 与特定sagas添加到nextjs组件中的函数,而没有太多代码,最好的是像这样:

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

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

1 个答案:

答案 0 :(得分:1)

combineReducers({reducers}),

最有可能的是你有reducer的字典,但是将它们包含在冗余对象中。在这种情况下,使用rest spread operator combineReducers({...reducers})或传递combineReducers(reducers)的简单对象。请参阅原始文档:https://redux.js.org/docs/api/combineReducers.html#arguments

如果reducers是数组,则应迭代它并为每个reducer分配自己的名称。