【问题标题】:Nextjs with redux redux-saga initialize storeNextjs 与 redux redux-saga 初始化存储
【发布时间】:2017-11-20 16:01:54
【问题描述】:

这样的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 并将其传递给每个组件,这并不难编码,但它是否高效?

【问题讨论】:

  • 那么解决了吗?我有同样的问题,但我无法从答案中理解。

标签: javascript redux redux-saga nextjs


【解决方案1】:
combineReducers({reducers}),

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

如果reducers 是数组,您应该遍历它并为每个reducer 赋予自己的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2019-03-03
    相关资源
    最近更新 更多