反应本土 – 领域和反应本土 – 实现自动更新的最佳做法?

前端之家收集整理的这篇文章主要介绍了反应本土 – 领域和反应本土 – 实现自动更新的最佳做法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
什么是最佳实践/模式在反应本机应用程序中作为反应数据源的领域?特别是 presentational and container components pattern

这是一个我想做出反应的例子:@L_502_1@

docs on auto-updates/change-events有点薄,official example没有使用这个功能(据我所知).

您可以通过订阅事件并在收到更改事件时更新ui,使您的示例无效.现在只有在写入事务被提交时发送事件,但将来会添加更细粒度的更改事件.现在,您可以添加以下构造函数来更新ui更改:
constructor(props) {
  super(props);
  this.realm = new Realm({schema:[dogSchema]})
  this.realm.addListener('change',() => {
    this.forceUpdate()
  });
}

您需要按住Realm实例来保持通知的生效,并且您可以在组件的其余部分使用该Realm实例.

而不是调用forceUpdate,您可以将事件侦听器中的组件状态或道具设置为触发刷新,如下所示:

constructor(props) {
  super(props);

  this.realm = new Realm({schema:[dogSchema]})

  this.state = {...}; // Initial state of component.

  this.realm.addListener('change',() => {
    this.setState({...}); // Update state instead of using this.forceUpdate()
  });
}
原文链接:https://www.f2er.com/react/300733.html

猜你在找的React相关文章