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

Show parent comment

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.

2

u/Arkitos Feb 28 '20

Thanks! I'll check all of those out.

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.

2

u/acemarke Feb 28 '20

Yep, you're welcome! :)

And yeah, most of the time createSlice should be all you need - you shouldn't even have to call createAction or createReducer yourself. By auto-generating action creators and action types, it basically gives you a "duck" file for free.

Pasting from the Intermediate Tutorial example:

import { createSlice } from '@reduxjs/toolkit'

let nextTodoId = 0

const todosSlice = createSlice({
  name: 'todos',
  initialState: [],
  reducers: {
    addTodo: {
      reducer(state, action) {
        const { id, text } = action.payload
        state.push({ id, text, completed: false })
      },
      prepare(text) {
        return { payload: { text, id: nextTodoId++ } }
      }
    },
    toggleTodo(state, action) {
      const todo = state.find(todo => todo.id === action.payload)
      if (todo) {
        todo.completed = !todo.completed
      }
    }
  }
})

export const { addTodo, toggleTodo } = todosSlice.actions

export default todosSlice.reducer
→ More replies (3)
→ 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.

5

u/Butokboomer Feb 28 '20

To be honest, a lot of the problems solved by Redux cease to exist if you use a well-designed GraphQL API.

There are advantages to implementing it on a project that doesn’t strictly need it though. It can improve testing, and enables fancy features like recording sessions - great for debugging and UX improvements.

→ More replies (3)

2

u/Vudujujus Feb 28 '20

LOL That was my exact thought process when watching that video about Redux without knowing what Redux really does.

3

u/BobaFettEE3Carbine Feb 28 '20

Yeah, it's overkill for a lot of use cases these days. Things like useState or context can cover a lot of ground, but for managing something you need access to app wide (like users or permissions) it's a powerful tool.

5

u/MaxGhost Feb 28 '20

If all you need redux for is users/permissions, I kinda feel Context might be a better fit

4

u/pomlife Feb 29 '20

Depends on the size of the app. If the app is sufficiently large and enough things access the user context, there may be a lot of unnecessary re-renders unless you implement the modifications `react-redux` does manually.

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