在React HOC中使用Redux连接

时间:2018-07-26 09:23:34

标签: reactjs redux connect

我对sessionTimeout有特殊要求。现在我要使用Redux操作方法进行Redux连接。

当我调用方法this.props.actions.logout时,它将引发错误Undefined。

我应该如何在HOC中连接redux

HOC

import React from 'react';
import { Modal } from 'antd';
import * as actions from '../../actions';
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

export default function(ComposedClass){
 return class extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      warningTime: 4000,
      signoutTime: 5000,
    };
  }

  componentDidMount() {
    this.events = [
      'load',
      'mousemove',
      'mousedown',
      'click',
      'scroll',
      'keypress'
    ];

    for (var i in this.events) {
      window.addEventListener(this.events[i], this.resetTimeout);
    }

    this.setTimeout();
  }

  clearTimeoutFunc = () => {
    if (this.warnTimeout) clearTimeout(this.warnTimeout);
    if (this.logoutTimeout) clearTimeout(this.logoutTimeout);
  };

  setTimeout = () => {
    this.warnTimeout = setTimeout(this.warn, this.state.warningTime);
    this.logoutTimeout = setTimeout(this.logout, this.state.signoutTime);
  };

  resetTimeout = () => {
    this.clearTimeoutFunc();
    this.setTimeout();
  };

  warn = () => {
    Modal.warning({
      title: 'Session Timeout',
      content: 'Your session is about to expire in 1 mintue',
    });
  };

  logout = () => {
    this.props.actions.logout()
  };

  render() {

    return (
      <div>
        <ComposedClass {...this.props} />
      </div>
    );

  }
}
}

1 个答案:

答案 0 :(得分:3)

您可以在HOC中连接组件,并从中返回已连接的组件

export default function(ComposedClass){

    class Component extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          warningTime: 4000,
          signoutTime: 5000,
        };
      }

      componentDidMount() {
        this.events = [
          'load',
          'mousemove',
          'mousedown',
          'click',
          'scroll',
          'keypress'
        ];

        for (var i in this.events) {
          window.addEventListener(this.events[i], this.resetTimeout);
        }

        this.setTimeout();
      }

      clearTimeoutFunc = () => {
        if (this.warnTimeout) clearTimeout(this.warnTimeout);
        if (this.logoutTimeout) clearTimeout(this.logoutTimeout);
      };

      setTimeout = () => {
        this.warnTimeout = setTimeout(this.warn, this.state.warningTime);
        this.logoutTimeout = setTimeout(this.logout, this.state.signoutTime);
      };

      resetTimeout = () => {
        this.clearTimeoutFunc();
        this.setTimeout();
      };

      warn = () => {
        Modal.warning({
          title: 'Session Timeout',
          content: 'Your session is about to expire in 1 mintue',
        });
      };

      logout = () => {
        this.props.actions.logout()
      };

      render() {

        return (
          <div>
            <ComposedClass {...this.props} />
          </div>
        );

      }
    }
    return connect(mapStateToProps, mapDispatchToProps)(Component)
}