简体   繁体   English

React 组件 - 在未渲染时也继续工作

[英]React component - continue working also when not rendered

I have a single page React app.我有一个单页 React 应用程序。 The App.js decides what page to show, according to information from the server. App.js 根据来自服务器的信息决定显示哪个页面。 There is a Stopwatch component, which rendered only with 2 of the pages.有一个 Stopwatch 组件,它仅使用 2 个页面呈现。 This is the code, from App.js:这是来自 App.js 的代码:

render() {
  switch (this.state.patientState) {
      case statesEnum.WAITING:
        main = <WaitingPage socketClient={this.socketClient} />;
        break;
      case statesEnum.TURN_ARRIVED:
        main = <TurnArrivedPage />;
        break;
      case statesEnum.END:
        return <EndPage />; // Because we don't need a stopper here...
      default:
        return <h1>Error...</h1>;
   }

   return (
     <main>
       <Stopwatch initialSeconds={this.state.seconds} initialMinutes={this.state.minutes} initiallHours={this.state.hours} />
       {main}
     </main>
   );
}

But I want the stopper to continue counting up, even when it's not rendered on the screen.但我希望塞子继续计数,即使它没有在屏幕上呈现。 What can I do?我能做些什么?

Thank's!谢谢!

So thank's to @developer, this the return of App.js 's render function:所以感谢@developer,这是App.jsrender function 的return

return (
  <main>
    <StopwatchManager
      showStopwatch={showStopwatch}
      initialSeconds={this.state.stopwatchInitialData.seconds}
      initialMinutes={this.state.stopwatchInitialData.minutes}
      initialHours={this.state.stopwatchInitialData.hours}
    />
    {main}
  </main>
);

StopwatchManager contains all the functionality of the stopwatch. StopwatchManager包含秒表的所有功能。 It always rendered, but it decides to actually show the stopwatch by the showStopwatch prop.它总是渲染,但它决定通过showStopwatch实际显示秒表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM