尽管为 createStore() 提供了 initialState,为什么我还是得到“Reducer [...] 在初始化期间返回未定义"? [英] Why do I get “Reducer [...] returned undefined during initialization” despite providing initialState to createStore()?

查看:11
本文介绍了尽管为 createStore() 提供了 initialState,为什么我还是得到“Reducer [...] 在初始化期间返回未定义"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 redux createStore 方法中设置了 InitialState,我将 InitialState 作为第二个参数

I had set InitialState in my redux createStore method ,and I corresponding InitialState as second arguments

我在浏览器中出错:

<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 有问题:

but I guess this initateState has something wrong:

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

但是如果我在我的每个子减速器中设置 initialState 它可以正常地做字.有问题?

but if I set initialState in my every sub reducer it can did word normally. Something wrong?

推荐答案

createStore() 中的 initialState 参数经常让人们感到困惑.它从来都不是一种手动初始化"应用程序状态的方法.唯一有用的应用程序是:

The initialState argument in createStore() is often tripping people. It was never meant as a way to "initialize" your application state manually. The only useful applications for it are:

  • 从 JSON 状态负载启动服务器呈现的应用程序.
  • 从保存到本地存储的状态恢复"应用.

这意味着您从不手动编写 initialState,并且在大多数应用程序中您甚至不使用它.相反,reducer 必须始终指定自己的初始状态,而 initialState 只是一种在您拥有现有序列化版本时预填充该状态的方法.

It is implied that you never write initialState manually and in most apps you don’t even use it. Instead, reducers must always specify their own initial state, and initialState is just a way to prefill that state when you have an existing serialized version of it.

所以这个答案是正确的:您需要在减速器中定义初始状态.将它提供给 createStore() 是不够的,并且不是在代码中定义初始状态的一种方式.

So this answer is correct: you need to define the initial state in your reducer. Supplying it to createStore() is not enough, and is not meant to be a way to define the initial state in code.

这篇关于尽管为 createStore() 提供了 initialState,为什么我还是得到“Reducer [...] 在初始化期间返回未定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆