简体   繁体   English

一段时间不活动后,React Redux更新(重新渲染)组件

[英]React Redux update (re-render) a component after a period of inactivity

How would you go about re-rendering a component (or multiple components) in a React Redux (Express / Mongo backend) app? 您将如何在React Redux(Express / Mongo后端)应用中重新渲染一个或多个组件?

I built an app where multiple (authenticated) users collaborate on a Todo list like dashboard in real-time (with chat-like Notes in place of Todo's), but wanted to force a re render after a period of inactivity (possibly with a setInterval in componentDidMount?) so if one user deletes a task, it will eventually refresh on all other users dashboards after a period of time. 我构建了一个应用,其中多个(经过身份验证的)用户在仪表板之类的Todo列表上实时进行协作(使用类似于聊天的Notes代替Todo),但是想要在一段时间不活动后强制重新渲染(可能使用setInterval)在componentDidMount中?),因此如果一个用户删除任务,则一段时间后它最终将在所有其他用户的仪表板上刷新。 I was thinking about combining forceUpdate with setInterval but keep reading everywhere that it should be avoided. 我当时正在考虑将forceUpdate与setInterval结合使用,但要继续阅读应避免的地方。

I am aware the real-time updates must be done with websockets, but I was wondering if aa simpler short-term solution could be used for this particular situation without resorting to websockets, since my app doesn't really need instantaneous updates. 我知道必须通过websockets进行实时更新,但是我想知道是否可以在不使用websockets的情况下针对这种特殊情况使用更简单的短期解决方案,因为我的应用程序实际上不需要即时更新。

Here is the main component (list of Notes): 这是主要组成部分(注释列表):

class NoteList extends Component {
  componentDidMount() {
    this.props.getNotes();
  }
}

You are right that the coolest way is use websocket to listen to change event. 没错,最酷的方法是使用websocket监听更改事件。 But for short-term, polling is not a bad idea. 但是从短期来看,投票并不是一个坏主意。 Just remember to clear the interval when your component is unmounted. 只要记住要清除卸载组件的间隔。

 componentDidMount() {
    this.props.getNotes();
    this.interval = setInterval(() => this.props.getNotes(), 60000);
 }

 componentWillUnmount() {
    clearInterval(this.interval);
 }

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

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