0

I have a reducer that is intended for handling notification banners.

const notifReducer = (state = { notifMessage: null, notifType: null, timeoutID: null },
  action
) => {
  switch (action.type) {
    case 'SET_NOTIFICATION':
      if (state.timeoutID) {
        clearTimeout(state.timeoutID)
      }
      return {
        notifMessage: action.notifMessage,
        notifType: action.notifType,
        timeoutID: null
      }

    case 'REMOVE_NOTIFICATION':
      return {
        notifMessage: null,
        notifType: null,
        timeoutID: null
      }
    case 'REFRESH_TIMEOUT':
      return {
        ...state,
        timeoutID: action.timeoutID
      }
    default:
      return state
  }
}

export const setNotification = (notifMessage, notifType) => {
  return async dispatch => {
    dispatch({
      type: 'SET_NOTIFICATION',
      notifMessage,
      notifType
    })
    let timeoutID = await setTimeout(() => {
      dispatch({
        type: 'REMOVE_NOTIFICATION'
      })
    }, 5000)
    dispatch({
      type: 'REFRESH_TIMEOUT',
      timeoutID
    })
  }
}


export default notifReducer

It works fully fine in the rest of my app, except in this one event handler that uses a try-catch. If I intentionally trigger the catch statement (by logging in with a bad username/password), I get "Unhandle Reject (Error): Actions must be plain objects. Use custom middleware for async action", but I am already using redux-thunk middleware!

  const dispatch = useDispatch()

  const handleLogin = async (event) => {
    event.preventDefault()
    try {
      const user = await loginService.login({
        username, password
      })
      // 
    } catch (exception) {
      dispatch(setNotification(
        'wrong username or password',
        'error')
      )
    }
  }

edit: here is my store.js contents

const reducers = combineReducers({
  blogs: blogReducer,
  user: userReducer,
  notification: notifReducer,
})

const store = createStore(
  reducers,
  composeWithDevTools(
    applyMiddleware(thunk)
  )
)
demiglace
  • 405
  • 1
  • 5
  • 12

1 Answers1

0

I hope your question is answered in a post already. Please check the below link

Error handling redux-promise-middleware