TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Two Weird Tricks with Redux

122 pointsby jlongsterabout 9 years ago

13 comments

tlrobinsonabout 9 years ago
I like the elegant simplicity of Redux, but it&#x27;s so low-level and definitely feels like people are still figuring the &quot;right&quot; way to do a lot of things.<p>I tweeted my feelings on this last week:<p><pre><code> 2015: reflux flummox redux marty alt fluxible… 2016: redux-actions redux-act redux-forms redux-promise redux-ui… </code></pre> <a href="https:&#x2F;&#x2F;twitter.com&#x2F;tlrobinson&#x2F;status&#x2F;717258992989306880" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;tlrobinson&#x2F;status&#x2F;717258992989306880</a>
评论 #11490459 未加载
评论 #11491506 未加载
BinaryIdiotabout 9 years ago
I don&#x27;t know much about Redux but these tricks seem like they should be less trick and more re-worked flow.<p>For the first one, if a burger is meant to be consumed differently than fries (serial versus parallel) they why are they using the same mechanism with the burger having a hacked on predicate , service type, dispatch, etc? Why wouldn&#x27;t the burger just have a workflow that makes sense for how its invoked? For instance if the burger is tied to the UI and a backend request that must happen one at a time then the UI would disallow or provide a way of queueing which I guess this sorta supports but it seems forced and doesn&#x27;t look like a normal flow.<p>The second part, ignoring async responses. I don&#x27;t understand this part. Why can&#x27;t you just replace your state and drop the existing handlers so nothing async from the previous state occurs? Does redux just not support this out of the box or is the author doing something very edge-case-y?<p>When I don&#x27;t want to handle an async response anymore I remove the handle&#x27;s association with the event it was going to handle. It works like this in native DOM, jQuery, even my own bias data point: msngr. But I&#x27;m curious why wouldn&#x27;t redux be able to handle the same case?
评论 #11488944 未加载
mdc2161about 9 years ago
I just started with redux-saga which uses ES6 generators to handle app control flow.<p>After first impressions, I&#x27;d say it&#x27;s worth a look if you&#x27;re running into anything near as complicated as talked about here. It&#x27;s very easy to reason about and test. gaearon - the creator of redux - seems to approve of it as well.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;yelouafi&#x2F;redux-saga" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;yelouafi&#x2F;redux-saga</a>
评论 #11491650 未加载
raxisabout 9 years ago
The isEatingBurger (isLoading) flag seems to be idiomatic in Redux and shows up in tutorials. In addition to preventing overlapping requests, it&#x27;s useful for spinners, disabling submit buttons, etc. It&#x27;s cool that you rolled the isLoading pattern into a service.<p>If you really want to pass around the promise, it&#x27;s doable. To save in your component state, return the promise from your thunk so it gets returned by dispatch (thom_nic just posted this also). To save in your store, dispatch a &#x27;save&#x27; action from the thunk with the promise as an argument. But I agree that it&#x27;s probably a bad design.<p>The request sequence IDs are useful for many situations where you need to order or cancel async requests. If you have multiple in-flight requests for an autocomplete field, you only want to set isLoading=false when the last request completes, and you don&#x27;t want the result of a later request to overwrite the result of an earlier request if the responses come back out-of-order.
评论 #11490201 未加载
indeyetsabout 9 years ago
My usual approach is to avoid middlewares for async-requests and to keep this stuff in a separate layer. For example, it can look like an ApiClient object with ES7 async-methods. Such methods would:<p>1. send &quot;Start&quot; action<p>2. await for result of fetch(&quot;<a href="http:&#x2F;&#x2F;example.com&#x2F;api&#x2F;call&quot;);" rel="nofollow">http:&#x2F;&#x2F;example.com&#x2F;api&#x2F;call&quot;);</a><p>3. send success or error action<p>this way redux is 100% synchronous and I have an obvious place for throttling requests, keeping trace of promises, etc.
emilongabout 9 years ago
In addition to redux-saga (<a href="https:&#x2F;&#x2F;github.com&#x2F;yelouafi&#x2F;redux-saga" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;yelouafi&#x2F;redux-saga</a>) which others have mentioned, redux-loop (<a href="https:&#x2F;&#x2F;github.com&#x2F;raisemarketplace&#x2F;redux-loop" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;raisemarketplace&#x2F;redux-loop</a>) is another interesting &quot;declarative&quot; approach to sequencing operations that uses promises. I&#x27;m not using either (yet), but I wrote up my notes on the differences between them and the thunk middleware approach recently (<a href="https:&#x2F;&#x2F;blog.boldlisting.com&#x2F;connecting-redux-to-your-api-eac51ad9ff89#.79d5lnivt" rel="nofollow">https:&#x2F;&#x2F;blog.boldlisting.com&#x2F;connecting-redux-to-your-api-ea...</a>). One aspect of these discussions that I find is lost or at least very implicit is the notion of storing metadata about the status of async operations as well as their result is elided together, but there&#x27;s reason to make mindful choices about whether or not to do that.<p>BTW, the &quot;Ignoring Async Responses&quot; section here is a useful pattern, even if you&#x27;re not working on browser developer tools ;). I&#x27;ve been working on a web app and have a polling timeout that I cancel on logout. Not quite the same, but logout triggers a reset of all the state obtained while authenticated, but the timeout ids are also in the store and canceled before being cleared.
rbalickiabout 9 years ago
What stuck out to me is<p>&quot;In an asynchronous world, you have 3 actions indicating asynchronous work: start, done, and error. In our system, they all are of the same type (like ADD_BREAKPOINT) but the status field indicates the event type.&quot;<p>This is a great suggestion (I have used a very similar schema before, although in flux), and should be the standard suggestion in redux tutorials.<p>Not enforcing this can lead to a proliferation of inconsistently named constants, especially if a team is learning flux&#x2F;redux and has not yet settled on a naming schema.
评论 #11490003 未加载
评论 #11489350 未加载
评论 #11489599 未加载
评论 #11489358 未加载
评论 #11489914 未加载
dyscreteabout 9 years ago
Can you tell me a use case using the &quot;action&quot; that&#x27;s sent to &quot;request.run&quot;, or is it passed just because it can be.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;gecko-dev&#x2F;blob&#x2F;master&#x2F;devtools&#x2F;client&#x2F;shared&#x2F;redux&#x2F;middleware&#x2F;wait-service.js#L51" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;gecko-dev&#x2F;blob&#x2F;master&#x2F;devtools&#x2F;cl...</a>
评论 #11490194 未加载
shopfaunt321about 9 years ago
Good stuff,<p>The EAT_BURGER seems slightly odd, as i&#x27;ve never hit a situation where I would allow a second one to be fired while the first is still pending. I.E. upon EAT_BURGER.start i would disable the UI for the EAT_BURGER action to be fired. I guess that&#x27;s an idealised world and you&#x27;ve hit something more complicated that requires it though, and the solution is good<p>I particularly like the async call tracking - very simple and neatly solves your issue!<p>Cheers
thom_nicabout 9 years ago
re: an action caller needing to wait on the async action to complete: I thought that was a non-issue. If you use redux-thunk and your action creator returns a promise, the caller gets it. So while you&#x27;re no longer triggering off of a redux action&#x2F;store state change -- which may or may not be seen as a good thing -- you <i>can</i> get the promise and add a `.then()` which will fire after the async action completes.<p>I put together a simple example here: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;thom-nic&#x2F;fbedf27a5222a8490aa9028a40bf053c" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;thom-nic&#x2F;fbedf27a5222a8490aa9028a40b...</a><p>Basically: component fires a &#x27;delete&#x27; action, and then does page navigation when delete is successful. (Not saying this is a good idea to do it that way, just that you <i>can</i> do it.) Or did I miss his point?
评论 #11490103 未加载
评论 #11490212 未加载
Kiroabout 9 years ago
&gt; I’ve used it a couple times to solve complex scenarios<p>I would love to know what those scenarios were.
评论 #11489394 未加载
cel1neabout 9 years ago
Why not keeping Async operations as react-elements (simple &lt;div&#x2F;&gt;s with hookds) in the DOM?
theknarfabout 9 years ago
If I need something with more features than Redux I would probably just use Rx.js.