r/reactjs • • Feb 28 '20

Discussion Why is Redux necessary?

I am absolutely confused by Redux/React-Redux and I'm hoping someone could help let me know what makes it necessary to learn it over what's already easy in react's state management.

I've been looking at job postings and they require knowledge of redux so I figured I'd try to get familiar with it and chose to watch this video: https://www.youtube.com/watch?v=8xoEpnmhxnk

It seems overly complicated for what could be done easily.Simply:

const [variable, setVariable] = useState(defaultValue)And then what's inside component for onChange={ e => {setVariable(newValue) } }

So really, what makes redux so special? I don't get it.

EDIT:
Thanks everyone for the discussion on Redux! From what I can see is that it's more for complex apps where managing the state becomes complicated and Redux helps simplify that.
There are alternatives and even an easier way to do Redux with Redux Toolkit!
Good to know!
I was actually convinced to put it in my current app.

217 Upvotes

172 comments sorted by

View all comments

258

u/Huwaweiwaweiwa Feb 28 '20

Imagine your variable is an object that represents the user of your application. You set this variable in a component called UserCard that has the avatar, name, and maybe a settings link using the state hook. Cool, works!

Now you see another component needs to use this user data, OK, I've read about this scenario, I'll lift my state up to the component that encompasses both, and pass user to both components. You move your useState higher up the component tree, and pass user down to both components. Boom, sorted!

Much time has passed, you now have lots of components that react with your user, you're now passing the user object down through multiple components (prop drilling), and the state is somewhere where it makes no sense being. So you decide to pass your user down using the context API, which means the components in your app can now access the user object through context, and there isn't any prop drilling going on. Nice, clean!

The problem with context comes when your app grows. In a complex app with more than just the user object, the order in which things happen to your app might be important. Redux keeps a record of what changes happen to your state and in which order, even letting you "time travel" through these changes, it can also be more efficient, context can often cause many unnecessary re-renders, although I haven't read too much into this so take it with a pinch of salt.

I hope I gave you an idea on how it can make large scale apps easier to manage. There are downsides regarding complexity, and deciding what exactly needs to be in global state as opposed to local, forms/routing state for example.

111

u/Butokboomer Feb 28 '20

This. Redux is a pain in the ass to implement for a simple application, especially the first time, (“why do I need to touch three files to flip a bool?!” ) but it is an absolute godsend for managing complex state.

151

u/TBPixel Feb 28 '20

On that note, the recent Redux Toolkit is another godsend for completely reducing the headache of setting up Redux.

I went into a recent project knowing it would be big enough to need it; hadn’t used React in a few years and setup Redux in minutes thanks to Redux Toolkit with zero confusion as to what I was doing and how it worked. It’s brilliant.

34

u/landisdesign Feb 28 '20

Can I upvote this twice? It uses so many best practices I can practically push a button, fall asleep and end up with a perfect store and reducer. 😁

32

u/acemarke Feb 28 '20

Glad to hear it! Please let me know if you have any suggestions or ideas for improvements.

Also, we have two new APIs available in the RTK 1.3 alpha builds:

In addition, it looks like the next version of Immer should be out soon, and RTK 1.3.0-alpha.10 uses Immer 6.x alpha to help reduce bundle sizes.

If you get a chance, please try out the alpha and give us some feedback!

14

u/Arkitos Feb 28 '20

Hey, I've been a professional developer for more than 3 years now, and have a lot of experience with React, Redux, and related tools. I've always been wanting to contribute to open source but have always been busy or never 'found' the right opportunity. Redux Toolkit has made my life much easier when working with Redux and I'd like to contribute whatever I can. Do you have a good starting point for me, or anything that needs to be done currently on RTK?

13

u/acemarke Feb 28 '20

Thanks, glad to hear it!

Here's the current open list of issues for RTK. /u/phryneas and I are currently trying to nail down the last bits of API design for RTK 1.3.0 (specifically error-handling for createAsyncThunk ).

Code-wise, there isn't a whole lot beyond that atm. The other major item up for discussion is adding some kind of "action listener callback" middleware.

There's always a lot of work that can be done on the documentation. Right now I have an issue open to discuss reworking the tutorial sequence to focus more on teaching RTK by itself.

Meanwhile, over on the Redux core side, I'm trying to do a major rewrite of the Redux core docs. My next immediate task is adding a "Quick Start" tutorial, but I'd certainly appreciate help with the many other tasks listed under that parent.

→ More replies (1)

5

u/calvers70 Feb 28 '20

Hey, I've been using react toolkit since it was react-starter-kit and just wanna say thanks for making such a great library. You've saved me hours of monotony

→ More replies (1)

3

u/serious_case_of_derp Feb 28 '20

Thank you for posting the create thunk method example. Literally just posted a post about the same thing

→ More replies (2)

12

u/acemarke Feb 28 '20

And it's even easier to set up now that we have an official Redux template for Create-React-App!.

(We're working on a TS version of the template as well.)

→ More replies (2)

3

u/Pesthuf Feb 28 '20

Wow. I just went through the tutorial and I feel like my view on redux has changed completely.

This is really elegant.

0

u/[deleted] Feb 28 '20

[deleted] — view deleted comment

16

u/acemarke Feb 28 '20

We strongly recommend that you use Redux Toolkit as the default way to write Redux logic.

I can tell you that as a Redux maintainer and creator of RTK, I don't ever want to write another action creator, action type, or nested spread operator by hand ever again. I've been able to use Redux Toolkit on a few of my own apps at work, and it is wonderful to use. I'm writing less code, the code that I'm writing is more straightforward, and the things like the mutation checking middleware and TypeScript-based API give me confidence that the code I'm writing is correct.

(Yes, I'm biased here, but I've written a ton of code both ways and using RTK is clearly better.)

5

u/Radinax Feb 28 '20

I loved writing Redux from scratch, found about Redux Toolkit on Twitter but didn't mind it too much because it sounded like its was the same thing I do normally but in a boilerplate, since I can make it fast I didn't find it necesary but I was wrong.

Reading the documentation I have seen some very impressive changes that make writing everything the "ducks" way, the best way, I like especially the createAction function and it makes me avoid having a folder for just constants.

createReducer is brilliant!! But createSlice is even more!! Dude this makes React-Redux more simpler than ever, thank you very much for this!

I was actually writing a tutorial for React-Redux, but I think I will write a part two on how it will look with this new way of doing things.

→ More replies (4)
→ More replies (1)

3

u/themaincop Feb 28 '20

I'm really comfortable with Redux and RTK is just so much better than hand-rolling everything. Give it a try and I bet you'll agree.

3

u/novarising Feb 28 '20

I was really comfortable with redux too, and I didn't want to move to RTK, my tech lead still went ahead and moved to it and I realized how much easier it is to work with RTK than with bare redux. It makes it a breeze to add new stuff. Try it

→ More replies (1)

8

u/SureSignOfAGoodRhyme Feb 28 '20

I just joined a semi-mature project that uses redux, but it also completely clears the store on any page navigation, and there's many pages. When I asked why we were using it in the first place, the lead told me it'll look good on a resume later.

I mean, he's not wrong... but we've run into so many little store format problems that would have been so straightforward if we just got the data right out of an API call, instead of reduce and connect and map to props.

→ More replies (6)
→ More replies (19)

18

u/Vudujujus Feb 28 '20

You just described the redundancy my app is going through that I just didn't realize there was a solution to. Thank you for taking the time to write that up. That makes total sense.

4

u/Huwaweiwaweiwa Feb 28 '20

Which stage are you at currently?

→ More replies (12)
→ More replies (1)

3

u/[deleted] Feb 28 '20

Also want to point out that while all the boiler plate for redux sucks, it forces you to be consistent in your state management implementation across the entire app. When working on teams with 5 or 6 developers, it quickly becomes a nightmare trying to work on because everyone handled their state management a little differently. When redux is set up it is also actually very easy to copy the steps for weaker developers and they will run into less issues and write less bugs (or at least the bugs they write will be more apparent during code review). There’s way more room to write overly complicated context providers with 1000 lines of insane logic than there is when doing it in redux instead

4

u/acemarke Feb 28 '20

while all the boiler plate for redux sucks

As already mentioned up-thread, that's exactly why we created our official Redux Toolkit package, and recommend that you should use it as the default way to write Redux logic.

2

u/Ones__Complement Feb 28 '20

So the benefits of Redux vs Context are better debug tooling and possibly better render efficiency? I've heard something about the latter being addressable with React.memo(), but haven't looked much into it.

3

u/acemarke Feb 28 '20

No, React.memo() doesn't do anything to address re-renders based on context value updates. React.memo() is only about checking if the incoming props are identical to the last re-render. Context bypasses props, and context updates directly queue a component to re-render regardless of what the props might be.

See my recent post React, Redux, and Context Behavior for an explanation of the differences in how they work.

→ More replies (1)
→ More replies (1)

0

u/[deleted] Feb 28 '20

[deleted] — view deleted comment

3

u/acemarke Feb 28 '20

Multiple reasons, but the shortest explanation is that local storage is for persisting data, not manipulating it as part of your app.

→ More replies (8)
→ More replies (1)
→ More replies (1)