TypeError:action $ .ofType(...)。mergeMap不是一个函数

Dar*_*ght 4 reactjs redux-thunk react-redux redux-observable

我是ReactJS的新手,正在尝试将redux与现有项目集成。

这是我index.js存储的文件

import 'rxjs'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { reducer as formReducer } from 'redux-form'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import { createEpicMiddleware, combineEpics } from 'redux-observable'

import user, { userEpic } from './user/duck'
import app from './app'

// Bundling Epics
const rootEpic = combineEpics(
    userEpic
)

// Creating Bundled Epic
const epicMiddleware = createEpicMiddleware(rootEpic)

// Define Middleware
const middleware = [
  thunk,
  promise(),
  epicMiddleware
]

// Define Reducers
const reducers = combineReducers({
  app,
  user,
  form: formReducer
})

// Create Store
export default createStore(reducers,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(), applyMiddleware(...middleware))
Run Code Online (Sandbox Code Playgroud)

这是 duck.js

const createUserEpic = (action$) =>
  action$
  .ofType(SIGNUP_CONCIERGE)
  .mergeMap((action) => {
    return Rx.Observable.fromPromise(api.signUpConcierge(action.payload))
    .flatMap((payload) => ([{
      type: SIGNUP_CONCIERGE_SUCCESS,
      payload
    }]))
    .catch((error) => Rx.Observable.of({
      type: SIGNUP_CONCIERGE_ERROR,
      payload: { error }
    }))
  })

export const userEpic = combineEpics(
  createUserEpic
)
Run Code Online (Sandbox Code Playgroud)

这让我出错 TypeError: action$.ofType(...).mergeMap is not a function

自从我更新了react,react-redux,redux-observable版本以来,我一直收到此错误。

我在这里做错了什么?请帮忙!!!

Ngu*_*You 6

尝试这个:

首先,将这些功能导入文件的最上方

import { mergeMap } from 'rxjs/operators';
import { ofType } from 'redux-observable';
Run Code Online (Sandbox Code Playgroud)

然后,像这样修复您的代码(请注意,ofType()mergeMap()comma,而不是分隔dot):

const createUserEpic = action$ =>
  action$.pipe(  //fixed
    ofType(SIGNUP_CONCIERGE),
    mergeMap(action => {
      return Rx.Observable.fromPromise(api.signUpConcierge(action.payload))
        .flatMap(payload => [
          {
            type: SIGNUP_CONCIERGE_SUCCESS,
            payload
          }
        ])
        .catch(error =>
          Rx.Observable.of({
            type: SIGNUP_CONCIERGE_ERROR,
            payload: { error }
          })
        );
    })
  );

export const userEpic = combineEpics(createUserEpic);
Run Code Online (Sandbox Code Playgroud)

您忘记了该pipe()方法,也忘记了从相应软件包中导入ofTypemergeMap方法的方法。

导入这些方法后,要使用它们,首先需要使用如下pipe()方法:

 action$.pipe();
Run Code Online (Sandbox Code Playgroud)

之后,您将可以使用ofType()mergeMap()方法:

 action$.pipe(
     ofType(),
     mergeMap()
 );
Run Code Online (Sandbox Code Playgroud)

请注意,它们之间用分隔comma,而不是dot