NGRX选择器多次返回相同的值

发布于 2025-01-29 01:54:24 字数 1024 浏览 4 评论 0原文

一个

export const selectIDs = creteSelector(
    selectorFeature,
    (feature: Feature) => {
        return feature?.metadata?.ids || []
    }
}

当系统启动时,我有一个组件中的

this.store.select(selectIDs).subscribe((ids) => dosomething(ids))

组件是undefined so []传递到回调中。然后功能然后更改为正常值,但.metadata不可用,因此选择与最后结果相同的returns [],因此dosomething()不应该回电称为。但是,实际上,它在我的代码中再次被调用[]

我的问题是,是否两个时间选择器返回[],是否应该调用一次calllback(subscribe中的这些调用)?

还是我的意思是,如果我这样做:

this.store.select(featueSelector).subscribe((feature) => console.log(feature)

假设全局状态更改了10次,但是功能保持不变,那么功能以元素为单位返回相同的功能,console.log()是否可以称为10次?我想只有在哪个功能Selectore返回与上次不同时,才会调用Console.log()?

谢谢

====================================== 事实证明this.store.select()返回可观察,因此,当观察者调用next()> console时。 log()将被调用。

NGRX选择器记忆意味着如果输入相同,它将直接返回相同的值。

I have a

export const selectIDs = creteSelector(
    selectorFeature,
    (feature: Feature) => {
        return feature?.metadata?.ids || []
    }
}

in the component I have

this.store.select(selectIDs).subscribe((ids) => dosomething(ids))

When the system is up feature is undefined so [] is passed into the callback. Then feature then changes to a normal value but .metadata not available so select returns [] which is same as last result so the dosomething() call back is not supposed to be called. But it actually get called again in my code with []

My question is if both time selector returns a [], shouldn't the calllback (those calls in subscribe)be called once?

Or generally I mean if I do this:

this.store.select(featueSelector).subscribe((feature) => console.log(feature)

suppose global state changes 10 times, but feature stays same so featureSelector returns same feature, will the console.log() be called 10 times? I imagine the console.log() will only be called when what feature selectore returns is different from last time?

Thanks

=================== updated =============
it turns out this.store.select() returns a observable, so when the obserable calls the next(), console.log() will be called.

Ngrx selector memorize means it directly returns same value if input is same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

謸气贵蔟 2025-02-05 01:54:24

选择器再次触发,并返回一个新的数组实例。
因为[] === []是虚假的,所以新实例传递给订阅方法。

作为解决方法,您可以:

  • 过滤我们的空数组:this.store.select()。pipe(filter(arr => arr.length> 0))。subscribe())
  • 使用独特的操作员:this.store.select()。pipe(dimstand(insertcheckhere))。subscribe())
  • 我认为您还可以创建一个变量const emptle = []并返回选择器中的空而不是[]

The selector is fired again, and a new array instance is returned.
Because [] === [] is falsy, the new instance is passed to the subscribe method.

As a workaround, you can:

  • filter our empty array: this.store.select().pipe(filter(arr => arr.length > 0 )).subscribe())
  • use the distinct operator: this.store.select().pipe(distinct(insertCheckHere)).subscribe())
  • I think you can also create a variable const empty = [] and return empty in the selector instead of []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文