useDispatch()无法与redux-observable

时间:2019-06-20 12:53:23

标签: reactjs redux redux-observable

在我的应用中将useDispatch钩子与async动作一起使用时,出现以下错误。

  

错误:动作必须是普通对象。使用自定义中间件进行异步   动作。

...有什么解决办法吗?

//my component >>>

const Comp: React.FC<any> = (props) => {
    const dispatch = useDispatch();
    const subscribers = useSelector((state: any) => state.subscribers.subscribers)

    return (
        <div>
            <h3>Alo Brasil</h3>
            <button onClick= 
{()=>dispatch(SubscribersTypes.GET_ALL_SUBSCRIBERS_START)}>Get All</button>
        </div>
    )
}

//my store >>>

    const epicMiddleware = createEpicMiddleware();
    const composeEnhancers = composeWithDevTools({});

    const configureStore = (preloadedState: ApplicationState) =>
       createStore(
          AppReducer,
          preloadedState,
          composeEnhancers(
             applyMiddleware(
                epicMiddleware
             )
          )
       );

    const store = configureStore(initialStateApp);

    epicMiddleware.run(AppEpic)

//my epic>>

const getAllSubscribersEpic = (action$: any) =>
    action$.pipe(
        filter(isActionOf(actions.getAllRequestStart)),
        switchMap(() =>
            from(axios.get(url))
                .pipe(
                    map((response: AxiosResponse) => response.data),
                    map((data: Array<Subscriber>) => actions.getAllRequestFinish(data)),
                    catchError(error => of(actions.subscribersError(error)))
                )
        )
    )

1 个答案:

答案 0 :(得分:0)

好吧,我的代码有错误... 我在我的史诗中添加了类型,现在可以使用了 如果有人经历过这个问题,我就把答案留在这里。

type Action = ActionType<typeof actions>;

const getAllEventsEpic : Epic<Action, Action, ApplicationState>= (action$) =>
    action$.pipe(
         ...
    )