【问题标题】:Normalizr Union is missing id of nested itemNormalizr Union 缺少嵌套项的 id
【发布时间】:2020-05-20 19:11:20
【问题描述】:

我定义了以下架构:

export const user = new schema.Entity('users', {}, { idAttribute: 'username' });

export const galleryImage = new schema.Entity('images', {
  user,
});

export const stream = new schema.Entity('streams', {
  user,
});

export const thing = new schema.Union({
  images: galleryImage,
  streams: stream,
}, (input, parent) => (parent.type === 'gallery_image' ? 'images' : 'streams'));

export const post = new schema.Entity('posts', {
  user,
  thing,
});
export const feedSchema = new schema.Array(post);

基本上,我的帖子方案“事物”是多态的,可以是 GalleryImage 或 Stream。它们是相同的,但我希望能够在标准化后区分它们,这似乎很容易做到。我的问题是,即使我已经定义了idAttribute,它在规范化后嵌套的thing 中是未定义的。

知道为什么会这样吗?

【问题讨论】:

  • 您能否添加一些示例数据以便您的代码可以运行?
  • 奇怪的是,当我在 Codebox 中尝试它时,它可以正常工作。呵呵。
  • 我设置了一个密码箱,但由于某种原因,它在那里工作,而不是我的本地设置:codesandbox.io/s/charming-tu-l57id
  • @PaulArmstrong 使用 JSON.stringify 似乎可以工作一次,但在 React 中不起作用。仍然在渲染中检测为未定义。
  • 对不起,console.log 好像骗了我,我的问题的原因是重新选择。

标签: reactjs redux normalizr


【解决方案1】:

我的问题实际上与 normalizr 无关。看来我最初的假设是错误的,因为 console.log 没有输出正确的值。

我以错误的顺序设置了我的派送,导致用户无法重新选择。这实际上是问题的原因。

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 2016-11-03
    • 1970-01-01
    • 2017-06-21
    • 2019-07-31
    • 2017-08-25
    • 2018-05-23
    • 2017-03-19
    • 2018-05-27
    相关资源
    最近更新 更多