MOBX可观察到只有在我记录的情况下才会更新
我有一个挂钩,它使用可观察到的映射 - code> conteralstore.connectedDevices
。该映射包含值也可观察到的值 - 传感器
当peripheralstore.connectedDevices
更改时,挂钩更新了,但是它们自己却没有。
有趣的是,一旦我登录了整个地图,钩子就可以重新租赁,并随着地图值的所有更改
我的自定义钩子:
export default function useSensor(): UseSensorObj {
const { peripheralStore } = useStore()
const devices = peripheralStore.connectedDevices
const isConnected: boolean = !_.isEmpty(devices)
const value = devices?.values().next()
//console.log(`log: ${JSON.stringify(devices)}`)
const batteryLevel = value?.value?.battery
return { devices, batteryLevel, isConnected }
}
这是我更新地图的方式:
class PeripheralStore implements IPeripheralStore {
constructor(...) {
makeAutoObservable(this, {}, { autoBind: true })
...
}
private async initSensor(sensor: ISensor) {
await runInAction(async () => {
sensor.init()
this.connectedDevices.set(sensor.peripheral.id, sensor)
})
}
...
}
这就是如何更新我更新对象的数据:
class Sensor implements ISensor {
constructor(...) {
makeAutoObservable(this, {}, { autoBind: true })
...
}
this.monitorCharacteristic(...) => {
runInAction(() => {
const battery = decodeByteArrival[17]
if (this.battery !== battery) {
this.battery = battery
}
...
})
}
...
}
I have a hook that uses an observable which is a map - peripheralStore.connectedDevices
. this map consists with values which are also observables - Sensor
The hook updates when peripheralStore.connectedDevices
changes, but the values themself aren’t.
Funny thing about it is that once i log the entire map, the hook get re-renders with every changes in the the map's values
My Custom Hook:
export default function useSensor(): UseSensorObj {
const { peripheralStore } = useStore()
const devices = peripheralStore.connectedDevices
const isConnected: boolean = !_.isEmpty(devices)
const value = devices?.values().next()
//console.log(`log: ${JSON.stringify(devices)}`)
const batteryLevel = value?.value?.battery
return { devices, batteryLevel, isConnected }
}
here is how i updated the map:
class PeripheralStore implements IPeripheralStore {
constructor(...) {
makeAutoObservable(this, {}, { autoBind: true })
...
}
private async initSensor(sensor: ISensor) {
await runInAction(async () => {
sensor.init()
this.connectedDevices.set(sensor.peripheral.id, sensor)
})
}
...
}
and here is how i update the object's data:
class Sensor implements ISensor {
constructor(...) {
makeAutoObservable(this, {}, { autoBind: true })
...
}
this.monitorCharacteristic(...) => {
runInAction(() => {
const battery = decodeByteArrival[17]
if (this.battery !== battery) {
this.battery = battery
}
...
})
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要重读文档的这一章 https:https://mobx.js。 org/peachite-ractivity.html#理解反应性
当您进行
JSON.STRINGIFY
时,它基本上会读取对象/数组或其他任何属性,因此此挂钩将在任何属性上更改。而且,当您没有JSON.STRINGIFY
时,您只读取顶级属性(数组/对象本身),因此钩子仅对该属性更改而不是内部反应。You probably need to reread this chapter of the docs https://mobx.js.org/understanding-reactivity.html#understanding-reactivity
When you do
JSON.stringify
it basically reads every property of your object/array or whatever, so this hook will rerender on any property change. And when you don't haveJSON.stringify
you only read top level property (array/object itself) so the hook only reacts to that property change, not inner ones.