开发者问题收集

TypeError:无法读取未定义的属性(读取‘类型’)-redux Reducer

2022-08-15
3605

reducers 的 index.js:

import { combineReducers } from "redux";
import authedUser from "./authedUser";
import users from "./users"
import tweets from './tweets';

export default combineReducers({
    authedUser,
    users,
    tweets
})

store:

 import React from 'react'
    import ReactDOM from 'react-dom'
    import './index.css'
    import App from './components/App'
    import {createStore,applyMiddleware} from 'redux'
    import {createLogger} from 'redux-logger'
    import {Provider} from 'react-redux'
    import reducer from './reducers'
    import middleware from './middleware'
    import logger from 'redux-logger'
    
    const store = createStore(
        middleware,
        reducer,
        applyMiddleware(logger)
      )
    ReactDOM.render(
    <Provider store={store}>
    <App />
    </Provider>, document.getElementById('root'))

错误相关的操作:

export const SET_AUTHED_USER = 'SET_AUTHED_USER'

export function setAuthedUser (id) {
  return {
    type: SET_AUTHED_USER,
    id,
  }
} 

reducer:

import { SET_AUTHED_USER } from '../actions/authedUser'

export default function authedUser (state = null, action) {
  switch (action.type) {
    case SET_AUTHED_USER :
      return action.id
    default :
      return state
  }
}

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

enter image description here

1个回答

这难道不是因为你以错误的顺序将参数传递给 createStore 造成的吗?

第一个参数应该是 Reducer。第二个参数是初始状态。由于你将 Reducer 作为初始状态传递,因此你会看到错误,即先前的状态是 Function 类型。

https://redux.js.org/api/createstore

Yuji 'Tomita' Tomita
2022-08-17