Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Reactjs中的页面中添加会话超时_Reactjs_Redux Form_Redux Thunk - Fatal编程技术网

在Reactjs中的页面中添加会话超时

在Reactjs中的页面中添加会话超时,reactjs,redux-form,redux-thunk,Reactjs,Redux Form,Redux Thunk,我想在我的登录页面中添加会话超时。在会话超时前几秒钟,页面应显示会话超时警报。在此之后,会话应该超时,用户应该被重定向到初始页面。请帮我做这个。 这是我的loginform.js页面 import React, { Component } from 'react'; import { connect } from 'react-redux'; import { login } from '../../redux/reducer'; import './LoginForm.css'; im

我想在我的登录页面中添加会话超时。在会话超时前几秒钟,页面应显示会话超时警报。在此之后,会话应该超时,用户应该被重定向到初始页面。请帮我做这个。 这是我的loginform.js页面

    import React, { Component } from 'react';
import { connect } from 'react-redux';
import { login } from '../../redux/reducer';
import './LoginForm.css';
import { BrowserRouter as Router, Route } from "react-router-dom";

class LoginForm extends Component {

  constructor(props) {
    super(props);
    this.state = {
       timeOut:false,
      warning:false,
    };
    this.onSubmit = this.onSubmit.bind(this);
  }


componentDidMount(){

    setTimeout(() => {
      this.setState({timeOut:true});
    },30000);//if 8sec is your warning time

    setTimeout(() => {
      this.setState({timeOut:true});
    },10000);//if 10sec is your time out
  }

  render() {
    let {username, password} = this.state;
    let {isLoginPending, isLoginSuccess, loginError} = this.props;
     if(this.state.timeOut){
     return  <Route to="/Home" push={true} />
    }
    else{
    return (
      <form name="loginForm" onSubmit={this.onSubmit}>
        <div className="form-group-collection">
          <div className="form-group">
            <label>username:</label>
            <input type="username" name="username" onChange={e => this.setState({username: e.target.value})} value={username}/>
          </div>

          <div className="form-group">
            <label>Password:</label>
            <input type="password" name="password" onChange={e => this.setState({password: e.target.value})} value={password}/>
          </div>
        </div>

        <input type="submit" value="Login" />

        <div className="message">
          { isLoginPending && <div>Please wait...</div> }
          { isLoginSuccess && <div>Success.</div> }
          { loginError && <div>{loginError.message}</div> }
        </div>
      </form>
    )
  }
}
  onSubmit(e) {
    e.preventDefault();
    let { username, password } = this.state;
    this.props.login(username, password);
    this.setState({
      username: '',
      password: ''
    });
  }
}
const Home = () => (
  <div>
    <h2>Welcome Admin </h2>

  </div>
);

const mapStateToProps = (state) => {
  return {
    isLoginPending: state.isLoginPending,
    isLoginSuccess: state.isLoginSuccess,
    loginError: state.loginError
  };
}

const mapDispatchToProps = (dispatch) => {
  return {
    login: (username, password) => dispatch(login(username, password))
  };
}
这是我的商店

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import reducer from './reducer';

const store = createStore(reducer, {}, applyMiddleware(thunk, logger));
export default store;

为了让会话超时正常工作,我需要做哪些更改?

一个非常好的最佳实践是创建一个高阶组件,您可以为每个想要具有超时功能的组件提供该组件。下面的Stackoverflow帖子中有一个非常类似的问题和解决方案:


他们正在使用plougsgaard提供的反应超时包:

No!-组件为reactjs和react native。两种框架都可以实现高阶组件。因此,它适用于您的用例
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import reducer from './reducer';

const store = createStore(reducer, {}, applyMiddleware(thunk, logger));
export default store;