score:2
There may be some fundamental misunderstandings about RxJS, so I would spend some time to really get a solid foundation on it or consider using something like redux-thunk instead if your async needs aren't more complex than this.
So here are some things to guide you:
- Your
map
is returning an Observable, which means you now have an Observable of Observables akaObservable<Observable>
which is almost certainly not what you want. - It's not clear what
Observable.of(socket.emit('verify', { token: 'suc' })))
is meant to do becausesocket.emit()
returns the socket itself, so you're emitting then mapping the action to an Observable of the Socket itself? - The
mapTo
usages are also probably not what you intend. The second one cancels the first, and again you're creating an Observable of Observable. So your epic is emitting (and hence dispatching) a stream of Observables, not actions, which is why you get the "actions must be plain objects" error from redux.
I'm hesitant to give you a solution, but I'll ask that you try to truly understand it rather than just copy-pasting. Maybe take a step back, try to forget all your current beliefs on how Rx works and start fresh? You then might have that "ah ha!" moment :)
I'm guessing you meant to do something like this:
export default function tokenVerifyRequestEpic(action$) {
return action$.ofType(TOKEN_VERIFY_REQUEST)
.do(() => {
socket.emit('verify', { token: 'suc' }));
})
.mergeMap(() =>
Observable.race(
Observable.fromEvent(socket, 'verifySuccess')
.mapTo({ type: TOKEN_VERIFY_SUCCESS }),
Observable.fromEvent(socket, 'verifyError')
.mapTo({ type: TOKEN_VERIFY_FAILURE })
)
);
}
Here it is with verbose inline comments:
export default function tokenVerifyRequestEpic(action$) {
// filter out all actions except TOKEN_VERIFY_REQUEST
return action$.ofType(TOKEN_VERIFY_REQUEST)
// perform the side effect of emitting the 'verify' message
.do(() => {
socket.emit('verify', { token: 'suc' }));
})
// One problem with this code is that it might not properly account for
// multiple concurrent TOKEN_VERIFY_REQUEST requests. e.g. How are those
// handled by the server? How should be they be handled in the UI?
// If it's not supposed to be possible, it might be useful to assert
// against that condition so that if it does accidentally happen you
// throw an error
.mergeMap(() =>
// Race between either a 'verifySuccess' or 'verifyError'
// This assumes one or the other will always happen, if not
// then you might want to add a timeout() or similar
Observable.race(
Observable.fromEvent(socket, 'verifySuccess')
.mapTo({ type: TOKEN_VERIFY_SUCCESS }),
Observable.fromEvent(socket, 'verifyError')
.mapTo({ type: TOKEN_VERIFY_FAILURE })
)
);
}
Source: stackoverflow.com
Related Query
- Redux Actions must be plain objects. Use custom middleware for async actions
- Redux Error Actions must be plain objects. Use custom middleware for async actions
- React Redux Saga: Actions must be plain objects. Use custom middleware for async actions
- Redux thunk - Error ยท Actions must be plain objects. Use custom middleware for async actions even with dispatch an object with key type
- Redux error: Actions must be plain objects. Use custom middleware for async actions
- Problem with redux middleware - Error: Actions must be plain objects. Use custom middleware for async actions
- react & redux with hooks: Actions must be plain objects. Use custom middleware for async actions
- React redux Actions must be plain objects. Use custom middleware for async actions
- Async Action Redux Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions
- Use custom middleware for async actions. Actions must be plain objects
- React-Redux: Actions must be plain objects. Use custom middleware for async actions
- Unit test: Actions must be plain objects. Use custom middleware for async actions
- redux Actions must be plain objects but I defined plain object
- CreateAsyncThunk Error: Actions must be plain objects. Use custom middleware for async actions
- React-Redux-Saga: Actions must be plain objects. Use custom middleware for async actions
- Actions must be plain objects while using Redux
- Actions must be plain object. Use custom middleware
- React-Redux - Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions
- React-Redux: Actions must be plain objects. Use custom middleware for async actions Error
- Actions must be plain objects. Use custom middleware for async actions.how to solve this
- react-redux Error: Actions must be plain objects. Use custom middleware for async actions
- redux-observable + socket.io: Actions must be plain objects. Use custom middleware for async actions
- Error: Actions must be plain objects. Use custom middleware for async actions.
- Actions must be plain objects. Use custom middleware for async actions - Lost Here
- Actions must be plain objects. Use custom middleware for async actions
- How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.?
- Error with redux-promise : Actions must be plain objects. Use custom middleware
- Redux-Thunk Error: Actions must be plain objects. Use custom middleware for async actions
- Redux Actions must be plain objects error
- Error: Actions must be plain objects. Use custom middleware for async actions. But I don`t have async functions
More Query from same tag
- Can I pass object of data in select option
- How to customize bootstrap 4 in reactjs app with reactstrap dependency?
- React hook useEffect returns empty array at first
- Material UI: How to update component style
- Material-UI: Make IconButton only visible on hover?
- ReactJS: How to export variables from useState hook?
- Sorting object array by a property value
- How to fix type warning for radius settings in React app
- Cannot assign to 'state' because it is a constant or a read-only property
- Issues changing state using buttons with React.js
- Correctly updating Object in React.js State without using spread operator
- How to apply the same row color when the row is expanded for a table
- FontAweSome, why? Type '"acquisitions-incorporated"' is not assignable to type 'IconName'
- Webpack throws error on React image import
- Why won't my img src be read when it's a prop?
- I have downloaded nodejs for react but when my powershell open to install external tools it gives error shown in the image:-
- How create a new input field row by clicking a button reactjs
- React Select Async
- Error: Invalid value of type string for mapStateToProps argument when connecting component ConnectedForm
- Cannot deploy ASP.NET MVC + React to Azure
- styled-components makes input range not work
- Handle multiple input value change within an object
- Why I can build my react app but cannot start it
- How to declare array as any: when using UseState hook?
- How to apply animation in React JS
- How to dynamically render an element after scrolling to a certain position in a page?
- Spreading props on a React component with Typescript
- React Hooks `useState` set function exhibits sync as well as async behaviour
- React: Function not reading updated global variable
- React "document not defined" when including script