redux流程给出错误

时间:2017-08-29 19:34:13

标签: javascript reactjs redux react-redux

我正在使用React / Redux应用程序,我已经使reducers更新并删除用户配置文件地址,我必须将错误消息和成功消息从这些reducers传递给组件,我正在创建具有成功消息的const,< / p>

当我在 updateAddressByBuyerIdAddressIdSuccess 中使用时,成功消息正常工作,但是如果使用成功消息 updateAddressByBuyerIdAddressIdSuccess ,也就是它在控制台中提供错误而我的代码没有编译,任何人都可以帮忙解决这个问题,我的代码和错误显示如下,提前致谢

// @flow
import { createReducer } from 'reduxsauce';
import I18n from '../i18n/I18n';
import Types from '../actions/Types';

// Init State
export const INITIAL_STATE = {
    addresses: null,
    errorMessage: null,
    successMessage: null,
    isLoading: false,
};

/* Generic error message */
const genericError = I18n.getText('error.generic', {}, 'Something went wrong');

/* Update an address */
export const updateAddressByBuyerIdAddressId = (state: Object = INITIAL_STATE) => ({
    ...state,
    isLoading: true,
    successMessage: null,
    errorMessage: null,
});

export const updateAddressByBuyerIdAddressIdSuccess = (state: Object = INITIAL_STATE, action: Object) => {
    const successMessage = I18n.getText('account.address-updated', {}, 'Address updated!');
    return {
        ...state,
        address: action.address,
        successMessage: successMessage,
    };
};

export const updateAddressByBuyerIdAddressIdFailure = (state: Object = INITIAL_STATE, action: Object) => ({
    ...state,
    errorMessage: action.errorMessage || genericError,
    isLoading: false,
});

/* Deleting an address */
export const deleteAddressByBuyerIdAddressId = (state: Object = INITIAL_STATE) => ({
    ...state,
    isLoading: true,
    successMessage: null,
    errorMessage: null,
});

export const deleteAddressByBuyerIdAddressIdSuccess = (state: Object = INITIAL_STATE, action: Object) => ({
     const successMessage = I18n.getText('account.address-deleted', {}, 'Address deleted!');
     return {
        ...state,
        address: action.address,
        successMessage: successMessage,
      }
});

export const deleteAddressByBuyerIdAddressIdFailure = (state: Object = INITIAL_STATE, action: Object) => ({
    ...state,
    errorMessage: action.errorMessage,
    isLoading: false,
});

export default createReducer(INITIAL_STATE, ACTION_HANDLERS);

错误屏幕

enter image description here

2 个答案:

答案 0 :(得分:3)

()移除deleteAddressByBuyerIdAddressIdSuccess括号。转换此

export const deleteAddressByBuyerIdAddressIdSuccess = (state: Object = INITIAL_STATE, action: Object) => ({
     const successMessage = I18n.getText('account.address-deleted', {}, 'Address deleted!');
     return {
        ...state,
        address: action.address,
        successMessage: successMessage,
      }
});

到这个

export const deleteAddressByBuyerIdAddressIdSuccess = (state: Object = INITIAL_STATE, action: Object) => {
     const successMessage = I18n.getText('account.address-deleted', {}, 'Address deleted!');
     return {
        ...state,
        address: action.address,
        successMessage: successMessage,
      }
};

在评论中添加了()等圆括号自动返回并且不需要关键字返回,但在您的情况下,您希望在返回之前设置const,因此您将其视为函数所以只使用了大括号{}。

答案 1 :(得分:1)

您将括号中的功能块的花括号包装起来,应该用于隐式返回一个对象(不带return关键字)。由于有问题的函数包含return关键字,因此不需要包含括号的前括号。

如下所示删除括号应解决错误:

export const deleteAddressByBuyerIdAddressIdSuccess = (state: Object = INITIAL_STATE, action: Object) => {
     const successMessage = I18n.getText('account.address-deleted', {}, 'Address deleted!');
     return {
        ...state,
        address: action.address,
        successMessage: successMessage,
      }
};