I know not to put side effects in reducers, and I know there are lots of great explanations about how to handle async actions. I have read them. I have a specific question I'm stumped on. Thanks!
I have state.largeObject
which is an object with many entries. I have a reducer that does some complex logic and merges the result into state.largeObject
like so:
export const myReducer = (state, { input }) => {
const largeObject = doSomethingComplex(input)
// other logic that uses largeObject
return {
...state,
largeObject: {
...state.largeObject,
...largeObject
}
}
}
I want to save the result of doSomethingComplex(input)
to the server. Where do I put the side effect? EDIT: without duplicating doSomethingComplex
which is still needed for other logic in the reducer.
myReducer
or doSomethingComplex
since they are pure. doSomethingComplex
both in the reducer and in the middleware.What am I missing? Thanks!
Middleware, thunks, and store subscription callbacks are all valid places to do that:
Based on the way you phrased things, I'm not sure if you're looking to send only the result of that call to the server, or the result of doing {...state.largeObject, ...largeObject}
.