NgRx ยกเลิกการสมัครจากร้านค้าในเอฟเฟกต์

ฉันมีผลง่าย ๆ :

@Effect()
simpleEffect = this.actions.pipe(
  ofType<ActionA>('Action A'),
  withLatestFrom(this.store.select(selectSomething)),
  map(([action, something]) => console.log('CALLED TWICE'))
);

นี่คือตัวเลือก:

export const selectSomething = createSelector(
  (appState) => appState.someState,
  (someState) => someState.something
)

ต่อไปนี้เป็นตัวลดอย่างง่ายซึ่งอัพเดตค่า something:


export const someReducer = (state: SomeState = someInitialState, action: ActionB) => {
  switch (action.type) {
    case 'Action B':
      return {
        ...state,
        something: action.payload
      };
    default:
      return state;
  }
};

ปัญหา:

เมื่อฉันส่ง ActionB เอฟเฟกต์สำหรับ ActionA จะถูกเรียก แม้ว่าจะไม่เป็นเช่นนั้นก็ตาม

ตามที่ฉันทราบ มันเกิดขึ้นเนื่องจากเอฟเฟกต์ withLatestFrom(this.store.select(selectSomething)) ซึ่งสังเกตการเปลี่ยนแปลงใดๆ ของ something FOREVER

คำถาม:

เป็นไปได้ไหมที่จะยกเลิกการสมัครจากตัวเลือกนั้น? หรือมีฟังก์ชันอื่นแทน withLatestFrom หรือไม่?


person Vlad Yurevich    schedule 09.11.2020    source แหล่งที่มา


คำตอบ (2)


ปัญหาไม่ได้อยู่ในโค้ดที่คุณแสดง เนื่องจาก withLatestFrom ปล่อยออกมาเท่านั้น เหตุการณ์หนึ่ง ติดตั้ง Redux DevTools เพื่อดูว่าคุณส่งการทำงานใดบ้าง และดูว่าคุณส่งหรือไม่ อย่าส่ง 'Action A' สองครั้ง

person Chris    schedule 09.11.2020
comment
ขอบคุณสำหรับคำตอบ! - person Vlad Yurevich; 10.11.2020

ปัญหาก็คล้ายกันแต่ไม่เหมือนกัน

จริงๆ แล้วฉันมีเอฟเฟกต์อื่นสำหรับ ActionC:

@Effect
otherEffect = this.actions.pipe(
  ofType<ActionC>('Action C'),
  switchMap(() => this.store.select(selectSomethingElse)),
  switchMap(() => of(new ActionA()))
)

และเรียกว่าเมื่อ somethingElse มีการเปลี่ยนแปลง

ฉันไม่ได้ส่ง ActionA โดยเจตนา แต่ simpleEffect ถูกกระตุ้นโดย otherEffect ซึ่งสมัครรับข้อมูลเมื่อ selectSomethingElse

person Vlad Yurevich    schedule 10.11.2020