加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:42:26 所属栏目:百科 来源:网络整理
导读:什么是最佳实践/模式在反应本机应用程序中作为反应数据源的领域?特别是 presentational and container components pattern? 这是一个我想做出反应的例子:Realm with React Native docs on auto-updates/change-events有点薄,official example没有使用这个
什么是最佳实践/模式在反应本机应用程序中作为反应数据源的领域?特别是 presentational and container components pattern?

这是一个我想做出反应的例子:Realm with React Native

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()
  });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读