简体   繁体   English

如何在redux-observable中使用“ of”

[英]How can i use “of” in redux-observable

I'm new to Redux-Observable. 我是Redux-Observable的新手。 So, I apply redux-observable in my project, and i want to dispatch action by redux-observable so i used "of" (like Observable.of() in RXJS version before). 因此,我在项目中应用了redux-observable,并且希望通过redux-observable调度操作,因此我使用了“ of”(就像之前RXJS版本中的Observable.of()一样)。 But the reponse that i received is "Actions must be plain objects. Use custom middleware for async actions". 但是我收到的答复是“动作必须是简单的对象。对异步动作使用自定义中间件”。 Is there a wrong with my set up epic middleware or code? 我设置的史诗中间件或代码有问题吗?

store.js store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { createEpicMiddleware } from 'redux-observable';
import { rootEpic } from './epics';
import reducers from './reducers';

const epicMiddleWare = createEpicMiddleware();
const configureStore = () => {
  const store = createStore(
    reducers,
    compose(
      applyMiddleware(epicMiddleWare),
      window.devToolsExtension ? window.devToolsExtension() : (f) => { return f; },
    ),
  );

  epicMiddleWare.run(rootEpic);

  return store;
};

export default configureStore;

epic.js epic.js

export const fetchNavigationEpic = (action$) => {
  return action$
    .ofType(actionTypes.FETCH_NAVIGATION_LIST)
    .pipe(
      mergeMap(() => {
        return from(CreateService(SettingService).getAll())
          .pipe(
            map((response) => {
              if (response.status === 200) {
                return of(fetchNavigationSuccess(response));
              }

              return fetchNavigationFailed(response);
            }),
          );
      }),
    );
};

action.js action.js

export const fetchNavigation = { type: actionTypes.FETCH_NAVIGATION_LIST };
export const fetchNavigationSuccess = (payload) => {
  return { type: actionTypes.FETCH_NAVIGATION_LIST_SUCCESS, payload };
};
export const fetchNavigationFailed = (payload) => {
  return { type: actionTypes.FETCH_NAVIGATION_LIST_FAILED, payload };
};

Library information: "redux-observable": "^1.0.0", "rxjs": "^6.2.1", "rxjs-compat": "^6.2.1", 库信息:“ redux-observable”:“ ^ 1.0.0”,“ rxjs”:“ ^ 6.2.1”,“ rxjs-compat”:“ ^ 6.2.1”,

The issue is that you're returning a stream not an action. 问题是您要返回的是流而不是操作。

If you're return an observable (of(yourAction)), you need to flatten that using mergeMap 如果返回一个可观察的(of(yourAction)),则需要使用mergeMap对其进行展平

If you're return the action, then you can use map instead of mergeMap 如果您要返回操作,则可以使用map而不是mergeMap

So it's either 所以要么

export const fetchNavigationEpic = (action$) => {
  return action$
    .ofType(actionTypes.FETCH_NAVIGATION_LIST)
    .pipe(
      mergeMap(() => {
        return from(CreateService(SettingService).getAll())
          .pipe(
            mergeMap((response) => {
              if (response.status === 200) {
                return of(fetchNavigationSuccess(response));
              }

              return of(fetchNavigationFailed(response));
            }),
          );
      }),
    );
};

Or 要么

export const fetchNavigationEpic = (action$) => {
      return action$
        .ofType(actionTypes.FETCH_NAVIGATION_LIST)
        .pipe(
          mergeMap(() => {
            return from(CreateService(SettingService).getAll())
              .pipe(
                map((response) => {
                  if (response.status === 200) {
                    return fetchNavigationSuccess(response);
                  }

                  return fetchNavigationFailed(response);
                }),
              );
          }),
        );
    };

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

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