【问题标题】:what is the issue mobx Error 'Reaction[observerobserved]?mobx Error 'Reaction [observerobserved] 有什么问题?
【发布时间】:2020-11-15 02:27:31
【问题描述】:

请帮我解决这个问题: 我正在使用 mobx-state-tree + React + mobx + Socket.io,我正在为项目编写聊天,并用于连接 socket.io 从服务器我得到聊天列表,然后使用动作把他放到 mobx-state-tree 存储,然后我想从 mobx-state-tree 存储获取数据并使用 map 函数渲染元素 但是,我发现了两个错误 这是我用于聊天的 mobx-state-tree 模型 enter image description here

这是我尝试映射数据的代码 enter image description here

这是问题

enter image description here

但是错误 "user" undefined 很奇怪,因为当我使用 console.log() 显示数据时,数组有这个数据并显示在控制台中

好的,我发现 Reaction[observerobserved] 有问题,这是因为渲染聊天列表的组件未包装到观察者函数中

但是当我在他身上调用 map 方法时,我仍然遇到未定义数组道具的问题

【问题讨论】:

  • 你找到了什么解决方案?
  • 是错误的,因为在这种情况下您尝试获取未定义的索引 'item.chat_users[index]",解决方案是使用 map 将 chat_users 数组映射到,然后为该数据渲染内部项目“item.chat_users .map(....)"

标签: reactjs socket.io mobx mobx-state-tree


【解决方案1】:

解决 |Reaction[observerobserved]" 需要将组件包装到observer() 函数中以订阅更改

对于第二个问题,使用未定义的道具:

第一个解决方案在屏幕上出错,因为您尝试在这种情况下获取未定义的索引“item.chat_users[index]”,解决方案是使用 map 将 chat_users 数组映射到,然后为该数据“item.chat_users”渲染内部项目。地图(....)”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2021-02-25
    相关资源
    最近更新 更多