React Redux - 从简单操作中调用函数会导致"not a function"错误



我在文件reduxer.js中有一个动作(已达到(称为" logoutuser((":

export const logoutUser = () =>
  (dispatch, getState) => {
    clearJwt()
      ({ type: LOGOUT_USER })
  }

i具有文件jwt.js中的函数" clearjwt((":

export const clearJwt = () => {
  localStorage.removeItem(TOKEN_KEY)
}

当它到达" clearjwt(("时,我会遇到此错误:

reducer.js:77 Uncaught TypeError: (0 , _jwt.clearJwt)(...) is not a function
at reducer.js:77
at index.js:11
at Object.logoutUser (LogoutContainer.jsx:28)
at LogoutContainer.componentDidMount (LogoutContainer.jsx:14)
at LogoutContainer.proxiedComponentDidMount (createPrototypeProxy.js:61)

jwt.js是按照reducer.js文件中的此行导入的:

import { saveJwt, getJwt, clearJwt } from '../auth/jwt'

我在其他位置尤其称此功能为一个捕获量,它出现在此功能上方的另一个功能中

    .catch(error => {
    clearJwt()
    dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message })
  })

..这有效!

但是,它在我的logoutuser((函数中抛出了上述错误。

它应该转到该功能,但它错误的是。

您正在调用clearJwt()的返回值:

clearJwt()
  ({ type: LOGOUT_USER })

SO clearJwt()被调用,但它返回undefined(因为它是一个块箭头函数,没有return语句(。然后将调用值(这是第二行(,但这会失败,因为当错误消息所述,"未定义不是函数"。

相关内容

最新更新