r/javascript • • Nov 10 '20

Stop using Redux for Data Fetching, use this instead

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

10 comments sorted by

21

u/Lyelinn Nov 10 '20

It’s not really fits for complex cases and “removing redux” after switching to fetching library? What?

Redux is a state handling solution first, not data fetching one. Someone there having 0 understanding how to use it properly.

2

u/franleplant Nov 11 '20

The post isn't about "removing redux", it is about using other solution for a particular use case instead of redux, in fact, the post says, and I quote, "redux has still valid use cases in your app".

Thanks for commenting!

2

u/Lyelinn Nov 11 '20

Yeah, I don’t mean whole article, it’s just one particular quote about guy removed redux completely with it lol.

8

u/acemarke Nov 10 '20

The article has some good points. Some of them are answered in the Redux "Style Guide" docs page, specifically things like "Use Redux Toolkit, "Use Immer", "Use Ducks files", etc.

As I pointed out in my recent JS Party podcast appearance, the real difference here is that Redux is not a purpose-built data fetching/caching solution, nor does it include anything out of the box for that use case.

FWIW, we're actually working on addressing that aspect right now :)

We've got an initial prototype of a Redux-based data fetching/caching solution in progress over at https://github.com/rtk-incubator/simple-query , which takes inspiration from a bunch of other libs that have tackled this problem space. It's not quite ready for public alpha testing, but if anyone would like to go take a look at what we've got and give us some feedback, that'd be appreciated!

0

u/franleplant Nov 11 '20

The final interface seems to solve some of the problems listed in the article but there's lot of boilerplate and you still have the library problem since you are depending on redux. In reality, with the coming of hooks you can probably build something like react-query based off of redux, but I don't believe it is worth it.

Have you checked redux-query?

3

u/acemarke Nov 11 '20

Uh... I'm sorry, I'm legitimately not sure what you're actually trying to say there by "final interface" and "library problem", at all.

Can you clarify what points you're trying to make?

Like I said, most of the bullet points in the blog post boil down to some combination of "I have to make decisions about how I write Redux code" and "Redux doesn't do any data fetching for me automatically". All I'm saying here is that RTK + our style guide addresses most of the "making decisions" aspect, and we're working on putting together a potential solution to the "automatic data fetching" aspect.

By "redux-query", are you referring to https://www.npmjs.com/package/redux-query?

2

u/franleplant Nov 11 '20

the "library problem" is the one that I talk about in the post, it can be sumed up as: "if you are building a lib that depends on redux you have one additional problem: consumer apps need to install and setup redux to satisfy your library".

By "final interface" (sorry for not being clear enough) I mean the interface you provide for components to say "hey I want to fetch this data".

Yes, that's the right redux-query.

I still believe that something like react-query is a much better solution, it requires much less code that what you showed, it is type safe, and then it solves all the problems you are trying to solve without redux and with a IMO better underlying storage implementation and semantic: a key value cache.

Also, this lib is pretty battle tested at this point, I have used it extensively in production (a lot of money goes through it) and so perhaps you want to consider exploring it before committing to this new impl.

Thanks for replying and sorry for not being clearer before.

Respectfully

Fran

2

u/acemarke Nov 11 '20

Thanks for clarifying that.

I had a Twitter discussion yesterday about the differences between "global stores" vs "server state caching", and whether the real issue is that "Redux is being used" or that "Redux doesn't do all the data fetching for you automatically":

https://twitter.com/acemarke/status/1326344484813230081

Question for the evening: Lots of discussion about "don't use Redux for server state, use OTHER_LIBS". To me, it seems like those libs are "global stores", just purpose-built for data fetching. Is the real point here about a "global store", or what the API does for you?
Put another way:

  • what makes a "server state caching lib" different than a "global store"?

  • if Redux Toolkit came with an equivalent data fetching abstraction, how would that change the suggested use cases and benefits of choosing between these tools?

Tanner Linsley commented:

In the context of #ReactQuery and tools like it, I don't think it matters what tech is backing the "store" as long as it's sound. I don't think users should or will care TBH as long as the public API is robust.
To proof this concept a smidge further, I could technically rebuild RQ to use redux/xstate/etc, but it likely wouldn't change the public API RQ has today, nor would it give it (or its users) any edge over it's functionality today.
So, something Redux-driven could easily offer the same experience, but I wouldn't expect it to be anything like RQ's API that is simply backed by Redux. I would expect it to be designed to take advantage of the Redux architecture and API (middlewares/reducers/dispatch/etc)

FWIW, I think there's several reasons why it's worth adding something official to Redux Toolkit at this point:

  • Data fetching abstractions seem to be where the ecosystem has headed in recent years. RTK's primary reason for existence is to address the common problems facing Redux users based on what they're already trying to do, and clearly data fetching is a pain point. I want us to address that.

  • While there's dozens of Redux-based data fetching addons already, RTK is intended to be an official opinionated set of defaults that work out of the box, without needing to install a bunch of other packages. Frankly, most people won't ever go looking for other addons in the ecosystem, but they will use RTK. Having something "official" and already included increases the odds they'd actually be able to use this.

  • Similarly, a lot of folks are already using Redux and aren't going to switch to react-query or anything else. I want to give them a path for improving their existing codebases without having to totally rip things out.

When you say "this lib is battle-tested", which one are you referring to - react-query by Tanner, or redux-query by Amplitude? I'm not actively developing this new RTK-based implementation myself, but I know the people working on it have looked at both react-query and redux-query in terms of features and implementation details to see what they do, how they work, and what we can learn from them. What specific aspects do you feel we should be focusing on?

1

u/franleplant Nov 12 '20

great discussion! thank you so much for participating.

So this is kind of a philosophical question if we go too deep into it but I believe that this lib (and probably any other lib) needs to think about its interface, if you can achieve a small api surface that solves the problem with minimal boilerplate then you win, to expand a little bit

  • the interface should be small (think react, it is a massive lib but with simple basic interfaces that allow all the goodies we enjoy)

  • the api should terse for common tasks

  • the api should have escape hatches when necessary and they should cover rare cases and it is fine for those to be verbose or a little bit more awkward

And finally but not least: conceptual clarity, when you build a program / lib you are programming moving concepts, if you can build clear strong concepts and then document them and use them consistently then you win. Concepts translate really well into code. Examples of this are react component, react hooks, react life cycle, react-query cache, or even go lower: a tree, an automata, etc (they strong concepts that hide the details behind it).

So TLDR: make the common use cases be clear and small and avoid verbosity and you win.

Sorry for the long answer.

2

u/allenksun Nov 12 '20

react-query is as easy as....const todosQuery = useQuery('todos', getTodos)

the fact that it's this simple is a major selling point. granted this is a basic implementation. reduce boilerplate as much as possible!

btw huge fan :)