开发者问题收集

未捕获的错误:Reducer“isClicked”在初始化期间返回未定义

2017-02-28
525

我尝试在 redux 中设置 isClicked 的初始状态,但总是抛出此错误

未捕获错误:Reducer“isClicked”在初始化期间返回未定义。如果传递给 Reducer 的状态未定义,则必须明确返回初始状态。初始状态可能不是未定义的。

这是我的代码

reducer

export default function reducerDomMethods(state={
isClicked: false,
}, action) {
switch (action.type) {
    case "CLICK_OPEN": {
        return {
            ...state,
            isClicked: true
        }
    }

    case "CLICK_CLOSE": {
        return{
            ...state,
            isClicked:false
        }
    }

        return state;
  }
}

action

export function clicking(isClicked) {

return function (dispatch) {

            if( isClicked === true){
                dispatch({type: "CLICK_OPEN",isClicked: true});
            }else {
                dispatch({type: "CLICK_CLOSE",isClicked: false});
            }
   }
}

combine Reducer

    import { combineReducers } from "redux"

    import cityName from "./apiReducers"
    import nameOfCity from "./apiReducers"
    import weatherDescription from "./apiReducers"
    import windSpeed from "./apiReducers"
    import temperature from "./apiReducers"
    import maxTemperature from "./apiReducers"
    import minTemperature from "./apiReducers"
    import isClicked from "./manMethodsReducers"

    export default combineReducers({
        cityName,
        nameOfCity,
        weatherDescription,
        windSpeed,
        temperature,
        maxTemperature,
        minTemperature,
        isClicked
    })

store

import { applyMiddleware, createStore } from "redux"

import logger from "redux-logger"
import thunk from "redux-thunk"
import promise from "redux-promise-middleware"

import reducer from "./reducers"
import reducerDomMethods from "./reducers"

const middleware = applyMiddleware(promise(), thunk, logger())

export default createStore( reducer , reducerDomMethods, middleware)

connect

import {connect} from "react-redux"

@connect((store) => {

return {
   nameOfCity:store.nameOfCity.nameOfCity,
   weatherDescription:store.weatherDescription.weatherDescription,
   windSpeed:store.windSpeed.windSpeed,
   temperature:store.temperature.temperature,
   maxTemperature:store.maxTemperature.maxTemperature,
   minTemperature:store.minTemperature.minTemperature,
   isClicked:store.isClicked.isClicked,
  }
 })

编辑:

我能够更正它(这是商店中的错误,没有给它文件的特定路径)。但现在我得到了

未捕获的 TypeError:无法读取 ReducerDomMethods 中未定义的属性“type”(manMethodsReducers.js:12)

这行代码是“switch (action.type) {

在 Reducer 中。我将“return”移到下面的一个括号中(正如您之前指出的那样)

2个回答

向您的 switch 添加一个 default 案例并从那里返回状态。

CharlieBrown
2017-03-01

默认情况下必须返回状态

Amir Tahani
2017-04-16