【问题标题】:Weird reducer redux hack奇怪的减速器 redux hack
【发布时间】:2021-01-29 10:56:29
【问题描述】:

所以我只是使用 redux 来调度一个动作,并且仍在适应使用钩子(我一直在为一些过时的项目工作;

我这样称呼我的行动

function generalFunction2(data) { 
        return { 
            type: constants.ACTION, data 
          } 
      }

    const generalFunction1 = (data, action) => {
        dispatch(generalFunction2(data))
    }

那么在我的实际减速器文件中,我尝试使用此代码

xport function generalFunction(state = initialState, data) {
    const data = action.value;
    const { type, value } = data;
    switch (action.typr) {
        case constants.ACTION:
            return {
                ...state,
                [type]: value
              };
            break;
        default:
            break;
    }
}

但我似乎在 const type can be undefined 上遇到了一个未定义的错误。

Uncaught TypeError: Cannot read property 'type' of undefined

所以我使用这个(我认为是 hack)有人有更聪明/更整洁的解决方法吗?

export function generalFunctionHack(state = initialState, data) {
    let type, value = "";
    if(action) {
        type = action.value.type;
        value = action.value.value;
    }
    switch (action.typr) {
        case constants.ACTION:
            return {
                ...state,
                [type]: value
              };
            break;
        default:
            break;
    }
}

【问题讨论】:

    标签: javascript node.js reactjs redux react-redux


    【解决方案1】:
    export function generalFunction(state = initialState, data) {
        const data = action.value; // you haven't defined action
        const { type, value } = data; // therefore data is undefined
        switch (action.typr) {}
    }
    

    获取 eslint,学习如何用好它。它会消除这样的错误。

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多