r/programming • • Nov 10 '20

Stop using Redux for Data Fetching, use this instead

https://nosleepjavascript.com/redux-data-fetching-antipattern/
0 Upvotes

12 comments sorted by

0

u/IceSentry Nov 11 '20

First of all, this subreddit isn't really for webdev or react.

Also, if you just used hooks and redux toolkit, as is recommended by the redux doc and as been recommended for a while, you would have realised that all your issues about verbosity and complexity are completely gone. The solution to fixing your problem isn't to avoid redux, it's to use it the way it's actually meant to be used in 2020. If it was 2016 and hooks weren't a thing then sure, that would be an interesting alternative, but we're not.

2

u/Lonely-Respond Nov 11 '20

Nope, react-query is much better than redux-toolkit for handling data fetching, I guess you haven't used it yet.

3

u/IceSentry Nov 11 '20 edited Nov 11 '20

I'm not trying to say redux is better, but half the article is complaining about redux boilerplate which is pretty much a non issue these days. Sure, react-query is most likely better for that particular use case, but the article really didn't do a good job at selling it as a better alternative. The suggestion of using react-query is almost a footnote of the article.

1

u/franleplant Nov 11 '20

that;s actually a good point, I mostly approached it via the LoC (lines of code) difference, I might want to do a follow up explaining a bit more about react-query.

Thanks for your feedback.

As I say in the article, even using react-toolkit you still have a lot of the boilerplate and decisions on your side to make, react-toolkit definitively makes it better but not much better, in fact I do link to a react-toolkit example of how to do something similar.

And it is also true that by using hooks you can solve the top level interface problem.

There are still problems left though as I mention in the post and in particular the data storage semantics (redux app state vs react-query's key value cache) make react-query a much better option IMO.

Also, react-query's interface is pretty good, and much better than anything I have seen around.

Thanks for chatting :)

2

u/IceSentry Nov 11 '20

After looking at it, I agree that for data fetching, react-query is most likely better in most situations than implementing it manually everytime. But in many situations people will already have redux for general state management anyway and you don't always need all the features provided by react-query.

Also, my point is that you spent a lot of time on talking about flaws that are solved by redux toolkit and hooks. Yes, you indees mentioned it, but only in a way that makes it sound as if it isn't how it's intended to be used. I'm sure u/acemarke would have a lot to say about that. Redux toolkit and ducks pattern are the recommended way of using redux so it feels a bit weird to claim there are many hard to chose options and to focus so much on the verbosity of it. For 99% of cases redux toolkit does everything you need and solves pretty much all your issues related to verbosity or which libraries to chose.

Also, in the apps I worked on I generally prefer to refetch the data rather than use a cache and have potentially stale data and since I already use redux anyway for other things it makes it harder to justify adding another dependency.

1

u/franleplant Nov 12 '20

fair points. One important aspect is that "programs are ever evolving things". So it is common to design a solution that will become a problem when the requirements change or our understanding of the problem changes, BUT, if we have battle tested solutions to a wide spectrum of problems and it fits our use case then it is of course a good idea to use it as it has more probability of being useful for a longer period of time.

What Im trying to say is that yes, you can go with redux a long way if your use case is simple enough, but the moment you have to deal with more complex scenarios you have to start rethinking and redoing and having problems and there is actually no hard extra cost I see of choosing a tool like react query from the start that you know will work well for the simple use cases and for the eventual more complex cases.

Redux-toolkit really does not solve the main problems I outlined, it makes them better, it provides better abstractions, but you still need to wire everything manually.

Fran

2

u/acemarke Nov 11 '20 edited Nov 12 '20

heh, we've had an entire discussion about this topic in the x-post thread over in /r/javascript:

https://www.reddit.com/r/javascript/comments/jrtg7n/stop_using_redux_for_data_fetching_use_this/

1

u/IceSentry Nov 11 '20

Oh, sorry about tagging you then. I missed that thread when I was doing my weekly reading of r/reactjs yesterday lol.

1

u/acemarke Nov 12 '20

oops, I actually typoed that comment there. the thread was actually in /r/javascript . linked it correctly, just wrote the wrong sub name above (fixed now).

but no worries, I appreciate it when people let me know there's a relevant discussion, especially if it's a sub I don't normally post in.

1

u/franleplant Nov 11 '20

am I breaking any rules by posting this post in this community? Do you have a source about that? if this was a mistake I sincerely apologize.

2

u/IceSentry Nov 11 '20

No there aren't any rules, this sub isn't even moderated anyway. My point is that a lot of people here have a lot of hate towards anything web/frontend related. I'd suggest posting to r/reactjs for a much nicer community that actually cares about react.

0

u/[deleted] Nov 10 '20

[deleted]

1

u/franleplant Nov 11 '20

a really nice solution too, react-query was inspired by it and takes a lot of the same semantics.