react-native - How to get data from rxjs?
问题描述
I have a file to save the random id to AsyncStorage.
AppService.js
import Rx from 'rxjs/Rx';
import uuid from 'uuid/v4'
import StorageService from '../storage/StorageService'
export default class AppService {
constructor() {
this.deviceuuid = null
}
rxInit() {
return StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
if (deviceuuid == null) {
this.deviceuuid = uuid()
console.log('deviceuuid is empty, create one')
return StorageService.shared.set('deviceuuid', this.deviceuuid)
} else {
this.deviceuuid = deviceuuid
return Rx.Observable.of(this.deviceuuid)
}
}).do((deviceuuid) => {
console.log(`deviceuuid is ${deviceuuid}`)
})
}
}
AppService.shared = new AppService()
StorageService.js
import Rx from 'rxjs/Rx'
import { AsyncStorage } from 'react-native'
import Storage from 'react-native-storage'
let storage = new Storage({
size: 1000,
storageBackend: AsyncStorage,
defaultExpires: null,
})
export default class StorageService {
set(key, value) {
return Rx.Observable.fromPromise(storage.save({key: key, data: value})).map(() => value)
}
get(key) {
return Rx.Observable.fromPromise(storage.load({key: key})).catch(() => Rx.Observable.of(null))
}
remove(key) {
return Rx.Observable.fromPromise(storage.remove({key: key})).catch(() => Rx.Observable.of(null))
}
}
StorageService.shared = new StorageService()
When I build the project I can see deviceuuid is 14c629ee-0dc7-43ef-91c2-eed642271670
Now I want to get the deviceuuid
from another screen.
componentWillMount() {
console.log('LoginScreen componentWillMount');
console.log('rxInit data =>', AppService.shared.rxInit())
let testId = StorageService.shared.get('deviceuuid')
console.log('testID', testId);
StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
console.log('deviceuuid', deviceuuid);
this.setState({ uuid: deviceuuid })
}).do((deviceuuid) => {
console.log(`LoginScreen deviceuuid is ${deviceuuid}`)
})
}
the result like the image
I have no idea how to get deviceuuid
.
and the code is not working. I can't see any console log result.
StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
console.log('deviceuuid', deviceuuid);
this.setState({ uuid: deviceuuid })
}).do((deviceuuid) => {
console.log(`LoginScreen deviceuuid is ${deviceuuid}`)
})
I'm not familiar with rxjs
, any help would be appreciated.
解决方案
推荐阅读
- python - 导入 Paraview 时,python 出现这个错误该怎么办?
- asp.net-core - Razor 页面 Datatables.net Ajax 回发
- python - pip install paramiko 出现错误 - ModuleNotFoundError: No module named 'zlib'
- azure - Spring Boot 使用 HikariCP 通过 JDBC 超时连接到 Vertica
- visual-studio-code - 如何以独立方式运行代码格式化程序(不执行 IDE)?
- html - Bootstrap 4.6 网格系统/Flexbox 不受父元素高度的限制?
- python - 如何在python tkinter中使用while循环实现多线程停止按钮
- angular - 从子组件在父组件中打开 Modal
- flutter - Flutter:[文件路径和名称].java 使用或覆盖了已弃用的 API
- python - 当父类定义了__call__时如何打印子类的签名