Live data from Hacker News

Learn how to use Redux step-by-step

github.com

21–30 of 33 posts

Re: Learn how to use Redux step-by-step

#21

Redux is unnecessary complexity. Anyone considering it, please do yourself a favor: check out an MVC framework and see how simple it is. Remember, when React first came out they described at as "React is the V in MVC." Adopt MVC in your project and watch your productivity soar. Note: Some people believe that MVC implies two-way data binding. This is false.

I've spent most of my time as a back end developer, occasionally dabbling in some front end code in various MVC frameworks. I always hated it and found it to be tedious. I recently took up react+redux for a new project and for the first time feel like I'm developing for the UI in a way that makes sense and feel like I'm able to accomplish things way faster than with other clunky frameworks.

Which Javascript MVC framework did you use? Some people who used Angular hated it and conclude that MVC is horrible. No, Angular is horrible, not MVC.

Re: Learn how to use Redux step-by-step

#22

Earlier quoted context omitted.

Redux is just one piece of the puzzle. Let me try to explain the problem it solves. Let's say if you are building a complex SPA with lot of UI components in the view and state changes impacts several parts of the view. You can build this view in several ways: * Plain javascript with jquery - We can surely achieve it but the code quickly becomes a soup of callbacks and events handlers. After a while the code becomes u…

None of these problems are unique to web development. How do you solve these problems in iOS? In iOS you use MVC, and the Application object holds the state as a tree. MVC has a better solution to all these problems.

I'll ask you the same question I asked the sibling: how do you trace and replay state changes during debugging?

('Cause this is why we use Redux.)

Re: Learn how to use Redux step-by-step

#23
post #18

Earlier quoted context omitted.

No, if you get/set methods that doesn't imply javascript with jquery! I am using React, but not ReactRouter or Redux. I hold my application state in a tree, and the tree nodes have get/set methods. My state tree is not immutable. I think "immutable way of changing state" is an oxymoron.

How do you trace and replay state changes when debugging?

Not the one being asked, but I usually go with breakpoints in Dev Tools.

Re: Learn how to use Redux step-by-step

#24
post #22

Earlier quoted context omitted.

None of these problems are unique to web development. How do you solve these problems in iOS? In iOS you use MVC, and the Application object holds the state as a tree. MVC has a better solution to all these problems.

I'll ask you the same question I asked the sibling: how do you trace and replay state changes during debugging? ('Cause this is why we use Redux.)

Being able to do so is certainly a cool feature. But I've built a lot of apps prior to Redux and never felt like state replays were a must for debugging.

There are various ways to debug state in a React app that uses setState. React devtools show each component's state, and you can combine that with breakpoint debugging as per to figure out how you got there.

Re: Learn how to use Redux step-by-step

#25

Earlier quoted context omitted.

I've spent most of my time as a back end developer, occasionally dabbling in some front end code in various MVC frameworks. I always hated it and found it to be tedious. I recently took up react+redux for a new project and for the first time feel like I'm developing for the UI in a way that makes sense and feel like I'm able to accomplish things way faster than with other clunky frameworks.

Which Javascript MVC framework did you use? Some people who used Angular hated it and conclude that MVC is horrible. No, Angular is horrible, not MVC.

Yeah, I spent some time with Angular as well as Backbone/Marionette

Re: Learn how to use Redux step-by-step

#26

Earlier quoted context omitted.

This was a really long article so you may probably missed my reasoning for using redux. I'm copying from the "Introduction to redux section" (which comes right after the "Introduction"): "I have to say that I got interested in redux because of all that buzz this framework generated. However, I got really interested in it when I understood how close its philosophy was to functional programming - this, when combined wi…

Sorry, to me that's an unsatisfying answer. You are not saying what problem redux is solving for you. "It is like functional programming" does not even attempt to explain what the problem being solved is.

To answer the "functional programming" aspect:

First, "pure functions" are generally agreed to be easier to test and easier to understand, because they only rely on their inputs, and don't modify anything outside of the function. In a real application of any kind, you realistically can't write the entire app as pure functions. But, writing more of your codebase as pure functions means more of it is easily testable and understandable overall.

Second, while the OOP vs FP debate is never-ending, FP does lead to some nice forms of reusability via composition.

With Redux, you are intended to write your "reducers" as pure functions. It's up to you whether those reducers have complex logic or simply return the values they were given in the actions, but the reducers (which control the actual state updates) should all be pure, and therefore easily testable and understandable. Reducers can also be composed together to add new behaviors, such as:

    const finalUsersReducer = undoable(resettable(originalUsersReducer));
In addition, writing pure functions with no side effects, in conjunction with dispatching plain object actions, is what makes Redux's time travel debugging feasible. (That's not to say it's impossible to implement time travel in other ways, just that those aspects of Redux's design make it very straightforward to implement time travel.)

Re: Learn how to use Redux step-by-step

#27

Earlier quoted context omitted.

Redux is just one piece of the puzzle. Let me try to explain the problem it solves. Let's say if you are building a complex SPA with lot of UI components in the view and state changes impacts several parts of the view. You can build this view in several ways: * Plain javascript with jquery - We can surely achieve it but the code quickly becomes a soup of callbacks and events handlers. After a while the code becomes u…

No, if you get/set methods that doesn't imply javascript with jquery! I am using React, but not ReactRouter or Redux. I hold my application state in a tree, and the tree nodes have get/set methods. My state tree is not immutable. I think "immutable way of changing state" is an oxymoron.

It depends on the complexity of the application. If I am happy with the performance profile with mutable state then going with immutable state is obviously an overkill. These optimizations matter when React spends lot of time diffing the virtual dom of the entire app component for every state change.

I don't have an example with mutable state but for React + Redux, click on the benchmark perf test page @ https://github.com/guptag/js-frameworks. With 1500 tickers added to the page and with an price update happening every 10ms, React/Redux updates the view within 5ms (on my machine) and consistently maintains 60fps. This is when React does the virtual diffing of only one ticker component whose state is changed and skipping the rest (because of React-Redux's immutability checks). It's going to take more time if React spends diffing the entire app for every update (unfortunately, I don't have a measurement with mutable state as I built this page to compare with AngularJS).

We can easily measure and make a call whether immutability is needed for an app or not.

Re: Learn how to use Redux step-by-step

#28

Earlier quoted context omitted.

So what is the problem solved by Redux? I don't see a simple explanation of that in your write up. Most people struggle when asked this question. Some people go on about "time travel" and other fancy things that have nothing to do with the business problem you're trying to solve. Other people talk about "single source of truth" as if that didn't exist before Redux.

One benefit is that you have explicit events that create specific state changes. Your app is a succession of these events that certainly help when debugging and thinking about the potential states your app can be in. Like other state management solutions, it also 'writes' all your shouldComponentUpdate methods for you.

Sounds like event sourcing, for ui.

Re: Learn how to use Redux step-by-step

#29
post #24
post #22

Earlier quoted context omitted.

I'll ask you the same question I asked the sibling: how do you trace and replay state changes during debugging? ('Cause this is why we use Redux.)

Being able to do so is certainly a cool feature. But I've built a lot of apps prior to Redux and never felt like state replays were a must for debugging. There are various ways to debug state in a React app that uses setState. React devtools show each component's state, and you can combine that with breakpoint debugging as per to figure out how you got there.

Yeah, you can use breakpoints, but...that's awful. Like, it feels like crap to do, you end up stuck in half-transactions and other nonsense because you have no better options. The use of Redux (or any other functionally-oriented action-based state system, I was writing them in Java and C# long before I ever used Redux) enables powerful stuff that, yeah, might not be a "must"--but being a "must" and being a transformatively powerful tool that requires very little cognitive overhead to leverage aren't that far off.

Re: Learn how to use Redux step-by-step

#30
post #23
post #18

Earlier quoted context omitted.

How do you trace and replay state changes when debugging?

Not the one being asked, but I usually go with breakpoints in Dev Tools.

Breakpoints cannot replay. They can pause and continue playing.

The inability to examine a time series of data after-the-fact and resume from any point makes this approach significantly, significantly worse. Like, I've done it just as you have, and I'd under no circumstances go back. It's so bad that I've basically built action-pattern systems in other languages (before using Redux, actually).

Functional tooling and composition with managed side effects is fucking awesome and doing otherwise verges on footgunning if it doesn't camp right out there.

Post reply on HN