本文介绍了尽管向createStore()提供了初始状态,为什么仍会收到“初始化期间未定义的归约器[...]"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在浏览器中遇到错误:

I got a error in browser:

<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 方法:

and I invoked configeStoremethod in Root.js:

 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

但是如果我在每个子减速器中设置 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 ,并且在大多数应用中甚至都不使用它.相反,reduce必须始终指定其自己的初始状态,而 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()提供了初始状态,为什么仍会收到“初始化期间未定义的归约器[...]"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 00:59