javascript - redux 中的 reducers 有些问题

查看:190
本文介绍了javascript - redux 中的 reducers 有些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

报错信息如下:

The previous state received by the reducer has unexpected type of "Function". Expected argument to be an object with the following keys: "posts", "sidebar"

相关代码:

store.js

import { createStore, applyMiddleware } from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'

import reducers from '../reducers/index'

const middleware = process.env.NODE_ENV === 'production' ? [ thunk ] : [ thunk, logger() ]

const store = createStore(reducers, applyMiddleware(...middleware), window.devToolsExtension ? window.devToolsExtension() : f => f)

export default store

reducers/index.js

import { combineReducers } from 'redux'
import posts from './posts'
import sidebar from './sidebar'

const rootReducer = combineReducers({
    posts,
    sidebar
})

export default rootReducer

reducers/posts.js

import {
    RECEIVE_POSTS,
    RECEIVEING_POSTS
} from '../containers/index'

const initialState = {
    posts: [],
    post: {},
    message: 'done'
}

const post = (state = initialState, action) => {
    switch (action.type) {
        case RECEIVE_POSTS:
            return Object.assign({}, state, {
                posts: action.posts
            })
            break
        case RECEIVEING_POSTS:
            return Object.assign({}, state, {
                message: 'loading'
            })
        default:
            return state
    }
}

export default post

我找了挺长时间的,也不知哪里出了问题。求大神解答

解决方案

我前两天找到了问题。。。原因是createStore的第二个参数是初始化的store。
写代码的时候手快忘记打了,蠢哭了 :(

这篇关于javascript - redux 中的 reducers 有些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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