javascript - 尽管向 createStore() 提供了 initialState,但为什么我得到的是 “Reducer [...] returned undefined during initialization”?

标签 javascript redux react-redux

我已经在我的 redux createStore 方法中设置了 InitialState,并且我相应的 InitialState 作为第二个参数

我在浏览器中遇到错误:

<code>Uncaught Error: Reducer "postsBySubreddit" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined.</code>

代码在这里:

import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import rootReducer from '../reducers/reducers'
import Immutable from 'immutable'
const loggerMiddleware = createLogger()
//const initialState=0
function configureStore() {
    return createStore(
    rootReducer,
     {postsBySubreddit:{},selectedSubreddit:'reactjs'},
     applyMiddleware(
     thunkMiddleware,
    loggerMiddleware
  )
 )
}
  export default configureStore

然后我在 Root.js 中调用了 configeStore 方法:

 import React, { Component } from 'react'
 import { Provider } from 'react-redux'
 import configureStore from '../store/configureStore'
 import AsyncApp from './AsyncApp'
 import Immutable from 'immutable'
 const store = configureStore()
 console.log(store.getState())
 export default class Root extends Component {
 render() {
   return (
     <Provider store={store}>
       <AsyncApp />
     </Provider>
  )
 }
}

但我猜这个initateState有问题:

import { combineReducers } from 'redux'
import {reducerCreator} from '../utils/creator'
import Immutable from'immutable'
import {SELECT_SUBREDDIT, INVALIDATE_SUBREDDIT ,REQUEST_POSTS, RECEIVE_POSTS} from '../actions/action'
let initialState=Immutable.fromJS({isFetching: false, didInvalidate: false,items:[]})

function selectedSubreddit(state, action) {
  switch (action.type) {
  case SELECT_SUBREDDIT:
    return action.subreddit
  default:
    return state
  }
}
function postsBySubreddit(state, action) {
  switch (action.type) {
    case INVALIDATE_SUBREDDIT:
    case RECEIVE_POSTS:
    case REQUEST_POSTS:
      return Object.assign({}, state, {
        [action.subreddit]: posts(state[action.subreddit], action)
      })
    default:
      return state
  }
}
function posts(state=initialState,action) {
  switch (action.type) {
    case INVALIDATE_SUBREDDIT:
      return state.merge({
        didInvalidate: true
      })
    case REQUEST_POSTS:
      return state.merge({
        isFetching: true,
        didInvalidate: false
      })
    case RECEIVE_POSTS:
      return state.merge({
        isFetching: false,
        didInvalidate: false,
        items: action.posts,
        lastUpdated: action.receivedAt
      })
    default:
      return state 
    }
}

const rootReducer = combineReducers({
  postsBySubreddit,
 selectedSubreddit
})
export default rootReducer

但是如果我在我的每一个 sub reducer 中设置 initialState 它可以正常执行 word。有问题?

最佳答案

我有同样的错误,但我没有包含默认情况

function generate(state={} ,action) {
  switch (action.type) {
    case randomNumber:
      return {
        ...state,
        random: action.payload
      }   
    default: // need this for default case
      return state 
   }
}

关于javascript - 尽管向 createStore() 提供了 initialState,但为什么我得到的是 “Reducer [...] returned undefined during initialization”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36619093/

相关文章:

javascript - Rails 如何 Gzip Javascript? (英雄)

javascript - React 组件之间的交互

reactjs - 导入已连接和已断开连接的组件 React、Redux

javascript - 有没有地方可以测试代码是否符合 ES5/安全?

javascript - redux-form 迁移到 v6 在 getValues.js 中抛出错误

javascript - react 错误: "Cannot update during an existing state transition"

javascript - React-router v4 this.props.history.push(...) 不起作用

reactjs - Redux Thunk, Sequelize (Bluebird) - 向 Action 创建者 "chain"返回 promise

reactjs - 在 React + Redux SPA SaaS 中处理超大型存储中的数据一致性

javascript - 如何同时使用 stacknavigator 和 tabnavigator?