Re:React Implementation Notes
by cageface on Tuesday October 11, 2016 @10:52AM
(#12684906)
My main hesitation with Redux is how awkward it is to insert async calls in the chain of events. Things like redux-thunk and redux-saga are impressive hacks but feel like a lot of gymnastics to do something that pretty much every React app needs to do.
[ Parent
| on HN ]
I dislike redux-thunk, because it breaks serialisability. I don't know redux-saga, but this is my way of doing it:
Create a data-structure like "running requests" in your state and put a serializable object in, could be complex or just a string "GET_USER_DATA".
Then write a simple "NetworkManager". React reconciles the UI to match the state, the NetworkManager does the same for your "running requests": make the actual running requests match those in the state.
In some projects I even (ab)used React to do that: add a component that renders a <div/> and manage the XHR in componentDidUpdate etc.
<Network>
<Get url="http://..."/ onLoad={() => state.GETsuccess(...)}>
</Network>
This way everything can be paused and is easy to reason about. You get cancellation and reuse of requests for free by using keys.