NGRX选择器多次返回相同的值
一个
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选择器再次触发,并返回一个新的数组实例。
因为
[] === []
是虚假的,所以新实例传递给订阅方法。作为解决方法,您可以:
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:
this.store.select().pipe(filter(arr => arr.length > 0 )).subscribe())
this.store.select().pipe(distinct(insertCheckHere)).subscribe())
const empty = []
and returnempty
in the selector instead of[]