• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Uncaught Error: Actions must be plain objects. Instead, the actual type was: ‘undefined’

Resolved: Uncaught Error: Actions must be plain objects. Instead, the actual type was: ‘undefined’

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m building a web application Reactjs and Redux, where you can login to dashboard and then you can logout. I’m using “redux-react-session” to manage sessions, and i created an action called logoutUser in userActions.js. this action is imported and called in Dashboard.jsx where i added a logout button with onClick event. What i’m expecting is when i click on logout it should simply redirects me to home page (‘/’)
My problem is: Whenever i click on logout button, it redirects me to home page (‘/’) then refreshing back to dashboard.
Console.dev error:

Uncaught Error: Actions must be plain objects. Instead, the actual type was: ‘undefined’. You may need to add middleware to your store setup to handle dispatching other values, such as ‘redux-thunk’ to handle dispatching functions.


userAction.js:
Dashboard.jsx:
Store.js:
Edit:
If i add return() in the action:
I get this error in Console.dev:
Edit (2):
Removing return ( ) => { … } and ( ) of logoutUser(navigate)( ) will keep the page refreshing then get me back to dashboard

Answer:

It seems the issue is related to using both the connect Higher Order Component and manually dispatching actions to the store. The connect HOC automatically wraps the action creators in a call to dispatch and injects a dispatchable action as a prop into the decorated component. Your code is then wrapping logoutUser in another call to dispatch. Pick one or the other, but not both.

Using connect HOC


Using useDispatch hook


The error message also seems to be saying that you haven’t yet added any asynchronous action middleware to your redux store.

You may need to add middleware to your store setup to handle dispatching other values, such as ‘redux-thunk’ to handle dispatching functions.


I suggest following the redux and react-redux guides for adding this to your store configuration, or update to redux-toolkit (same maintainers) which includes the thunk middleware out-of-the-box since it is such a common use case.

If you have better answer, please add a comment about this, thank you!

reactjs redux redux-thunk
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to convert Java bytecode to Webassembly using CheerpJ compiler

24/03/2023

Resolved: Is pandas groupby() function always produce a DataFrame with the same order respect to the column we group by?

24/03/2023

Resolved: Kivy widget hierarchy not behaving as expected

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.