未捕获错误:减速机" isClicked"在初始化期间返回undefined

时间:2017-02-28 23:46:52

标签: javascript reactjs ecmascript-6 redux react-redux

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

未捕捉错误:减速机" isClicked"在初始化期间返回undefined。如果传递给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;
  }
}

动作

export function clicking(isClicked) {

return function (dispatch) {

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

结合减速器

    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
    })

存储

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)

连接

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中未定义的内容(manMethodsReducers.js:12)

这是这一行"开关(action.type){"

在reducer中。我移动了"返回"下面的一个括号(如前所述)

2 个答案:

答案 0 :(得分:0)

在您的交换机中添加default个案并从那里返回状态。

答案 1 :(得分:0)

您必须在默认情况下返回状态