MOBX可观察到只有在我记录的情况下才会更新

发布于 2025-01-27 02:29:33 字数 1453 浏览 2 评论 0原文

我有一个挂钩,它使用可观察到的映射 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2025-02-03 02:29:33

您可能需要重读文档的这一章 https:https://mobx.js。 org/peachite-ractivity.html#理解反应性

mobx对执行跟踪函数时读取的任何现有可观察属性反应。

“读取”是在删除对象的属性,可以通过“点入” it(例如user.name)或使用括号符号(例如user ['name'],todos [3])或破坏(例如const {name} =用户)。

当您进行JSON.STRINGIFY时,它基本上会读取对象/数组或其他任何属性,因此此挂钩将在任何属性上更改。而且,当您没有JSON.STRINGIFY时,您只读取顶级属性(数组/对象本身),因此钩子仅对该属性更改而不是内部反应。

You probably need to reread this chapter of the docs https://mobx.js.org/understanding-reactivity.html#understanding-reactivity

MobX reacts to any existing observable property that is read during the execution of a tracked function.

"reading" is dereferencing an object's property, which can be done through "dotting into" it (eg. user.name) or using the bracket notation (eg. user['name'], todos[3]) or destructuring (eg. const {name} = user).

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 have JSON.stringify you only read top level property (array/object itself) so the hook only reacts to that property change, not inner ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文