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

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

您将如何在 React Redux(Express / Mongo 后端)应用程序中重新呈现一个组件(或多个组件)?

我构建了一个应用程序,其中多个(经过身份验证的)用户实时协作处理待办事项列表(如仪表板)(用类似聊天的注释代替待办事项),但想在一段时间不活动后强制重新渲染(可能在 componentDidMount 中有一个 setInterval?)所以如果一个用户删除了一个任务,它最终会在一段时间后在所有其他用户的仪表板上刷新。我正在考虑将 forceUpdate 与 setInterval 结合使用,但继续阅读应该避免的所有内容。

我知道实时更新必须通过 websockets 完成,但我想知道是否可以针对这种特殊情况使用更简单的短期解决方案而不求助于 websockets,因为我的应用程序并不真正需要即时更新。

这里是主要成分(注释列表):

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

你是对的,最酷的方法是使用websocket来监听变化事件。但从短期来看,轮询并不是一个坏主意。请记住在卸载组件时清除间隔。

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

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