【问题标题】:Navigate to specific page after a given time of user inactivity在给定的用户不活动时间后导航到特定页面
【发布时间】:2017-12-04 19:17:28
【问题描述】:

在用户不活动的给定时间段后,我正在尝试在 React 应用程序中导航或显示主页组件。

我正在使用 automaticRefresh 函数,它通过 DOM 事件检测空闲时间并在 2 分钟后重新加载应用程序。 index.js 文件中调用函数。

自动刷新

export default function () {
  let timer;

  window.onload = timerReset;
  document.onkeypress = timerReset;
  document.onmousemove = timerReset;
  document.onmousedown = timerReset;
  document.ontouchstart = timerReset;
  document.onclick = timerReset;
  document.onscroll = timerReset;
  document.onkeypress = timerReset;

  function timerElapsed() {
    window.location.reload();
    // Navigate or show Home page component
  };

  function timerReset() {
    clearTimeout(timer);
    timer = setTimeout(timerElapsed, 2 * 60 * 1000); // 2 mins
  }
}

正如您在timerElapsed 函数中看到的那样,我设置了页面重新加载并且工作正常,但是我有一个新任务要更改,可以导航到主页。 已经在 App 容器中,我有我的所有组件逻辑,StatementHome 组件要显示。

应用容器

import React, { Component } from 'react';
// Components
import Header from './components/Header';
import Home from './components/Home';
import Statement from './components/Statement';

class App extends Component {
  render() {
    const statement = this.props.statement !== null;

    return (
      <div>
        <Header />

        { statement ? <Statement /> : <Home /> }
      </div>
    );
  }
}

那么如何在没有路由器的 React 中在一段时间内不活动后显示 Home 组件?

【问题讨论】:

    标签: javascript reactjs redirect components


    【解决方案1】:

    我发布了回复,但之后意识到您不想使用路由器。在这种情况下,您执行 window.location = "http://yoururl.com"。

    这只有在它是一个真正的目的地而不是一个反应路线时才有效。如果它是你想要命中的反应路由,你需要一个路由器。

    【讨论】:

    • 已经试过了。它正在工作,但也会重新加载页面,在我的情况下不需要那个。应用程序必须有单页应用程序的感觉,所以我认为解决方案是在某些情况下显示或隐藏主页组件。路由器也是很好的解决方案,但我有要求没有它。
    猜你喜欢
    • 2016-07-15
    • 2016-12-27
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多