首页 > 解决方案 > 简单的 react-redux 连接函数中的错误

问题描述

尝试使用 react-redux 的“连接”功能时收到以下错误:

"TypeError: react__WEBPACK_IMPORTED_MODULE_4___default.a.memo is not a function"

此外还有以下细节:

wrapWithConnect
C:/.../node_modules/react-redux/es/components/connectAdvanced.js:325
  322 | } // If we're in "pure" mode, ensure our wrapper component only re-renders when incoming props have changed.
  323 | 
  324 | 
> 325 | var Connect = pure ? React.memo(ConnectFunction) : ConnectFunction;
  326 | Connect.WrappedComponent = WrappedComponent;
  327 | Connect.displayName = displayName;
  328 | 

我还没有找到与我的问题相关的这个错误的任何参考......

这是我在“Chat.js”类末尾使用“connect”的代码:

import { connect } from 'react-redux';


  ...


const mapStateToProps = state => ({
  ...state
});

const mapDispatchToProps = dispatch => ({
  addMessage: () => dispatch(addMessage)
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Chat);

此代码用于名为“Chat.js”的类中。当使用“导出默认聊天”而不使用 redux“连接”时,一切正常。

我使用提供者标签作为“App.js”中聊天组件的包装器:

import { Provider } from 'react-redux';
const App = class extends React.PureComponent {
  render() {
    const { title } = this.context;
    return (
      <div className="center-screen">
        {title}
        <Provider store={configureStore()}>
          <Chatroom />
        </Provider>
      </div>
    );
  }
};

行动:

export function addMessage(text) {
  return { type: 'addMessage', text };
}

减速器:

export default (state, action) => {
  switch (action.type) {
    case 'addMessage':
      return {
        text: action.text
      };
    default:
      return state;
  }
};

标签: reactjsreduxreact-redux

解决方案


也共享 webpack 文件。这样我就可以检查 webpack 设置中是否有任何问题


推荐阅读