【问题标题】:How do I listen to '@@router/LOCATION_CHANGE' action from typesafe redux-observable epic如何从 typesafe redux-observable epic 中收听“@@router/LOCATION_CHANGE”动作
【发布时间】:2021-10-08 11:22:37
【问题描述】:

我正在尝试收听来自 typesafe redux-observable epic 的 '@@router/LOCATION_CHANGE' 动作,但我只是不明白如何。

  1. 我需要过滤的具体操作是什么?我尝试'filter(onLocationChanged)' 失败。
  2. 什么是正确的'IN' 类型?使用 'LocationChangeAction''filter(onLocationChanged)' 我得到类型错误:

没有重载匹配这个调用。

  1. 最后,如何通过'@@router/LOCATION_CHANGE' 操作更改位置? (之前和之后)
import { Epic } from "redux-observable";
import { of } from "rxjs";
import { filter, withLatestFrom, concatMap } from "rxjs/operators";
import { AppState } from "../../reducer";
import { CallHistoryMethodAction, LOCATION_CHANGE, push, onLocationChanged, RouterActionType, LocationChangeAction } from 'connected-react-router';


type IN = LocationChangeAction;
type OUT = ReturnType<typeof somthing>;

export const LocationListenEpic: Epic<IN | OUT, OUT, AppState> = (
    action$,
    state$
) => 
    action$.pipe(
        filter(onLocationChanged),
        withLatestFrom(state$),
        concatMap(([{ payload }, state]) => {

            console.log(currentLocation);
            return []
}

【问题讨论】:

    标签: typescript react-router redux-observable


    【解决方案1】:

    在深入了解 redux-observable epics 的工作原理后:

    
    import { getLocation } from 'connected-react-router'
    
    type IN = LocationChangeAction;
    type OUT = ReturnType<typeof somthing>;
    
    export const LocationListenEpic: Epic<IN | OUT, OUT, AppState> = (
        action$,
        state$
    ) => 
        action$.pipe(
            filter(action => action.type == LOCATION_CHANGE),
            withLatestFrom(state$),
            concatMap(([{ payload }, state]) => {
    
                console.log(getLocation(store.getState()));
                return []
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      相关资源
      最近更新 更多