首页 > 解决方案 > “无法读取未定义的属性‘类型’”在 redux 存储中用于外部包中定义的操作

问题描述

作为我正在进行的学习 React 项目的一部分(我本身就是一个 ASP.NET 人),我遇到了这个问题。我有一套 React 应用程序,我想在其中使用一些常见的 UI 元素,所以我试图将它们分解成一个单独的 npm 包。对于共享组件本身,这工作得很好。

但是,其中一些组件依赖于 redux 操作来操作,所以我尝试将这些操作和一个 reducer 函数捆绑到外部包中。这是我的简化版本actions\index.js

export const SNACKBAR_MESSAGE = "SNACKBAR_MESSAGE";
export const SNACKBAR_HIDE = "SNACKBAR_HIDE";

export function showSnackBarMessage(message) {
    console.log('hit 1');
    return (dispatch, getState) => {
        console.log('hit 2');
        dispatch(hideSnackBar());
        dispatch({
            type: SNACKBAR_MESSAGE,
            message: message
        });
    }
}

export const hideSnackBar = () => {
    type: SNACKBAR_HIDE
};

这是reducer\index.js

import { 
    SNACKBAR_MESSAGE,
    SNACKBAR_HIDE
} from "../actions";

const initialState = {
    snackBarMessage: null,
    snackBarVisible: false
};

export default function UiReducer(state = initialState, action) {
    switch(action.type) {
        case SNACKBAR_MESSAGE:
            return Object.assign({}, state, { 
                snackBarMessage: action.message,
                snackBarVisible: true
            });
        case SNACKBAR_HIDE:
            return Object.assign({}, state, { 
                snackBarMessages: '',
                snackBarVisible: false
            });
        default:
            return state;
    }
}

这与作为原始项目的一部分运行良好的代码相同。这些由我的包的入口点文件导出,如下所示:

// Reducer
export { default as uiReducer } from './reducer';

// Actions
export { showSnackBarMessage as uiShowPrompt } from './actions';
export { hideSnackBar as uiHidePrompt } from './actions';

然后在我的消费项目中,我的默认减速器如下所示:

import { routerReducer } from 'react-router-redux';
import { combineReducers } from 'redux';
import { uiReducer } from 'my-custom-ui-package';
// Import local reducers

const reducer = combineReducers(
  {
    // Some local reducers
    ui: uiReducer
  }
);

export default reducer;

问题是当我尝试调度从我的外部包导入的这些操作之一时。我包含该操作,例如import { uiShowPrompt } from "my-custom-ui-package";并像这样发送它,dispatch(uiShowPrompt("Show me snackbar"));然后我看到两个控制台消息(hit 1hit 2)显示,但随后出现以下错误:

未捕获的类型错误:无法读取未定义的属性“类型”

at store.js:12
at dispatch (applyMiddleware.js:35)
at my-custom-ui-package.js:1
at index.js:8
at middleware.js:22
at store.js:15
at dispatch (applyMiddleware.js:35)
at auth.js:28
at index.js:8
at middleware.js:22

商店本身是这样的:

import { createStore, combineReducers, applyMiddleware, compose } from "redux";
import thunk from 'redux-thunk';
import { browserHistory } from "react-router";
import {
  syncHistoryWithStore,
  routerReducer,
  routerMiddleware
} from "react-router-redux";
import reducer from "./reducer";

const loggerMiddleware = store => next => action => {
    console.log("Action type:", action.type);
    console.log("Action payload:", action.payload);
    console.log("State before:", store.getState());
    next(action);
    console.log("State after:", store.getState());
};

const initialState = {};

const createStoreWithMiddleware = compose(
  applyMiddleware(
    loggerMiddleware, 
    routerMiddleware(browserHistory), 
    thunk)
)(createStore);

const store = createStoreWithMiddleware(reducer, initialState);

export default store;

恐怕我不明白这个错误。除了本质上将相同的代码从我的本地项目移动到 npm 包之外,我看不出我在做什么不同。由于 action 和 reducer 实际上都不依赖于 redux,因此我的 npm 包本身并不依赖于react-redux. 那是问题吗?如果还有什么我可以分享来帮助你帮助我,请告诉我。就像我说的那样,我对这一切还很陌生,所以很明显有些事情我做得不对!

标签: javascriptreactjsnpmreduxreact-redux

解决方案


问题可能出在hideSnackBar函数的声明中

export const hideSnackBar = () => {
    type: SNACKBAR_HIDE
};

这里的函数试图从Arrow Function返回一个Object Literal。这将始终返回undefined。由于解析器不会将两个大括号解释为对象文字,而是作为块语句。因此,无法读取 undefined as store 的属性“类型”的错误期望具有属性类型的操作。

替换这样的代码,看看它是否有效。

export const hideSnackBar = () => ({
    type: SNACKBAR_HIDE
});

括号强制它解析为 Object Literal。希望这可以帮助


推荐阅读