在react-native 应用程序中,哪些最佳实践/模式使realm成为react 式数据源?特别是presentational and container components pattern人?
我想举一个例子:Realm with React Native
docs on auto-updates/change-events有点薄,official example没有利用这个功能(据我所知).
在react-native 应用程序中,哪些最佳实践/模式使realm成为react 式数据源?特别是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()
});
}
您需要保留一个领域实例来保持通知的活动性,并且您可以在组件的其余部分使用这个领域实例.
您可以在事件侦听器中设置组件的状态或props 来触发刷新,而不是调用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()
});
}