最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get current value of ngrx selector without subscription - Stack Overflow

programmeradmin3浏览0评论

I have my ngrx state this way

state.data state.dataArchived

I would like to copy data from store.data to state.dataArchived.

selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch));
ngOnInit() {
  this.store.dispatch(new GetDirectionsOnGraph({}));
  this.selectedSchedulingsOnPopup$.subscribe(value => {
    this.selectedSchedulingsOnPopupValue = value;
    this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
  });
}

the problem is when state.data changes, state.dataArchived changes too

So I would like to get the current value of state.data without subscribing to it.

I have my ngrx state this way

state.data state.dataArchived

I would like to copy data from store.data to state.dataArchived.

selectedSchedulingsOnPopup$ = this.store.pipe(select(selectSchedulingsByBranch));
ngOnInit() {
  this.store.dispatch(new GetDirectionsOnGraph({}));
  this.selectedSchedulingsOnPopup$.subscribe(value => {
    this.selectedSchedulingsOnPopupValue = value;
    this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
  });
}

the problem is when state.data changes, state.dataArchived changes too

So I would like to get the current value of state.data without subscribing to it.

Share Improve this question asked Jan 6, 2020 at 16:55 infodevinfodev 5,22522 gold badges81 silver badges152 bronze badges 4
  • Do you only need the first value that is returned by selectedSchedulingsOnPopup? – wentjun Commented Jan 6, 2020 at 18:33
  • I need the value when the ponent is rendered, so I suppose that is the first value when subscribe, and only the first value – infodev Commented Jan 6, 2020 at 21:50
  • Ahh... In that case this is similar to a question I have previously answered (stackoverflow./a/59419097/10959940). Let me know if it makes sense? If it still doesn't, I will provide an answer – wentjun Commented Jan 7, 2020 at 0:24
  • Does this answer your question? NgRx Get value without subscribing – James D Commented Jan 7, 2020 at 9:50
Add a ment  | 

2 Answers 2

Reset to default 11

I don't understand the question, but if you want to get the value without subscribing to it, you can convert it into a promise.

async ngOnInit() {
  const data = await this.store.pipe(select(selectorForData),
                               take(1)).toPromise();
}

Keep in mind, that in this promise way, it will only react once and get the data and carry on. If you want to be informed of the changes of the data slice, you have to subscribe to it, there is no other way around it.

toPromise is marked as deprecated now. You can use firstValueFrom or lastValueFrom/take(1) to transform observable into promise as below.

async ngOnInit() {
      // firstValueFrom
      const data = await firstValueFrom(this.store.pipe(select(selectorForData));
      // lastValueFrom with take(1)
      const data = await lastValueFrom(this.store.pipe(select(selectorForData),
                                   take(1)));
}

If you just want the first value and then to ignore all subsequent changes use the first() operator:

  ngOnInit() {
    this.store.dispatch(new GetDirectionsOnGraph({}));
    this.selectedSchedulingsOnPopup$.pipe(first())
      .subscribe(value => {
        this.selectedSchedulingsOnPopupValue = value;
        this.store.dispatch(new GetDirectionsOnGraph(this.selectedSchedulingsOnPopupValue));
      });
  }
发布评论

评论列表(0)

  1. 暂无评论