【问题标题】:Store error using React.js - TypeError: Object(...) is not a function react使用 React.js 存储错误 - TypeError: Object(...) is not a function react
【发布时间】:2023-12-19 18:16:01
【问题描述】:

我正在尝试做我的 SPA 商店。我制作了我需要的每件东西的减速器,但是当我渲染页面时,我收到了这条消息

TypeError: Object(...) 不是函数 模块../src/redux/store.js C:/Users/Mycomputer/Desktop/Projects/React/2020_03_29_workshop/src/redux/store.js:13

13 |导出默认 createStore(rootReducers,composeWithDevtools(applyMiddleware(thunk)))

reducers.js

import { GET_ALL_POSTS, GET_ALL_SPECIALITIES, GET_ALL_COURSES, GET_ALL_TEACHERS,
    GET_POST, GET_SPECIALITY, GET_LESSON, GET_COURSE } from "./actions"

export const postReducer = (state = {}, action) => {
    if(action.type === GET_ALL_POSTS){
        return {
            ...state,
            posts: action.posts
        }
    }

    if(action.type === GET_POST){
        return {
            ...state,
            post: action.post
        }
    }
    return state
}

export const specialityReducer = (state = {}, action) => {
    if(action.type === GET_ALL_SPECIALITIES){
        return {
            ...state,
            specialities: action.specialities
        }
    }

    if(action.type === GET_SPECIALITY)
        return {
            ...state,
            speciality: action.speciality
        }

    return state
}

export const courseReducer = (state = {}, action) => {
    if (action.type === GET_ALL_COURSES){
        return {
            ...state,
            courses: action.courses
        }
    }
    if (action.type === GET_COURSE){
        return {
            ...state,
            course: action.course
        }
    }

    return state
}

export const teacherReducer = (state = {}, action) => {
    if(action.type === GET_ALL_TEACHERS){
        return {
            ...state,
            teachers: action.teachers
        }
    }

    return state
}

export const lessonReducer = (state = {}, action) => {
    if (action.type === GET_LESSON){
        return {
            ...state,
            lesson: action.lesson
        }
    }
    return state
}

store.js

import { createStore, combineReducers, applyMiddleware } from 'redux'
import { composeWithDevtools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'
import { postReducer, 
    specialityReducer, 
    courseReducer, 
    teacherReducer, 
    lessonReducer 
} from './reducers'



export default createStore(combineReducers({postReducer, specialityReducer, courseReducer, teacherReducer, lessonReducer}),composeWithDevtools(applyMiddleware(thunk)))

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    尝试将对象传递给 combineReducers 并在所有 reducer 之前给出名称。 combineReducers({ reducerName: reducerFunction });

    【讨论】: