简体   繁体   English

useDispatch()不适用于redux-observable

[英]useDispatch() not work with redux-observable

I am getting the following error when using useDispatch hook with async actions in my app. 在我的应用程序中使用useDispatch hook和async操作时,我收到以下错误。

Error: Actions must be plain objects. 错误:操作必须是普通对象。 Use custom middleware for async actions. 使用自定义中间件进行异步操作。

... any solution? ......任何解决方案?

//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)))
                )
        )
    )

Ok there was an error in my code... I've added Types in my Epic and now it works I leave here the answer if someone goes through this. 好吧,我的代码中出现了错误...我已经在我的史诗中添加了类型,现在它可以工作了,如果有人经历这个,我会留下答案。

type Action = ActionType<typeof actions>;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM