简单的react-redux连接功能

时间:2019-04-28 17:56:33

标签: reactjs redux 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“连接”时,一切正常。

我将provider标记用作“ 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;
  }
};

1 个答案:

答案 0 :(得分:0)

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