Why we needed Redux in the past, and don’t any more
hackernoon.com
hackernoon.com
I guess it'll be more project work for those who get brought in to bail these projects out.
Most things that people build
don't need anything like this.
True. It would be great to show examples of websites that benefit from the complexity of an extra library to handle client side state. I would think less then 1% do.For example, lets look at how HN handles state syncing. Function "toggle" in this file handles collapsing/expanding threads:
https://news.ycombinator.com/hn.js
function toggle (ev, id) {
var on = !afind($(id), collapsed());
(on ? addClass : remClass)($(id), 'coll');
recoll();
if ($('logout')) {
new Image().src = 'collapse?id=' + id + (on ? '' : '&un=true');
}
ev.stopPropagation();
return false;
}
How would the react+redux approach look like? Is it really worth it?I see React as a tool for building web applications, not websites. Using it for something like hacker news is not needed nor worth it, except for learning purposes or for the sake of using your favorite tech (which has its pros and cons).
Edit: I use React a lot, and really like it. It's just not the perfect tool for everything, and by no means a necessary one (at least when you don't have much complexity); even if it is a good one in many cases.
In the case of HN, seeing the current js [1] I find it pretty obvious that no "big tool" like React is necessary. It doesn't mean using React for a HN clone is a bad thing, one just don't have to.
[1]: https://news.ycombinator.com/item?id=17915560&goto=news
How do you define those terms?
This is totally personal and subjective, but I'd say that a website is defined by its content, while a web application is defined by its interaction with the user. That is, a website can plausibly consist of a static content repository that's dealt out to all visitors, while a web application depends on interaction and requires programmatic user input and data processing.
For example, a news site would be a "website", but a spreadsheet or a collaborative calendar would be web "applications". The news site shows essentially the same information to all visitors, while the calendar processes individual data.
Practically, most websites with quickly changing content will also rely on a sophisticated programmatic (and/or database) backend, but at least in principle they're only defined by their output. The web application on the other hand is essentially a program that runs remotely, and it depends fundamentally on a processing and a data storage backend.
Wordpress's backend is a web application. That web application exists to drive the website that is its frontend. Not really a controversial standpoint, I don't think, except amongst the hyper-literal who but-for about comment features (hence the use of "focus" above).
Don’t be a dick.
I could use Vue, but I'm already using and continuing to use React, and I'm good enough at (what isn't actually) my job (because I'm no frontend developer, this is just what I do on the side) to minimize its impact on end users.
I also use React for fully static sites via Gatsby, though I don't love it, and to quickly build HTML mail templates. Granted, the bigger value here for me is JSX rather than React, but for my use cases they're functionally the same thing, and I do think you can get a lot out of using it. Especially if elsewhere in the stack you're already undertaking more complexity with it.
Admittedly it's a bit of a beef of mine, but most React apps are very MVC. I even blogged about it a while ago: https://blog.talkjs.com/how-react-brought-model-view-control...
I can totally see the use case for React for web apps which provide a lot of user interaction on the frontend. But for all other web apps it doesn't seem to make sense as you also have to handle quite a few disadvantages (e.g. SEO unfriendly, user can't copy URL and send to a friend)
Am I missing something?
You can even use data from your Flask based backend directly, to create the static pages.
Actually, I also tried to move to GraphQL and still haven't a good fit yet. GraphQL queries are hard to translate to efficient SQL, and there are additional problems to tackle like authorization which is much complicated than the REST model.
So what would be a 1 line request in REST format is now 100x longer and complex and why? It's an internal service so we weren't worried about reducing bandwidth costs (the problem FB was presumably trying to solve). I assume it was adopted because it's sexy and/or the developer wanted an excuse to use it.
I also wonder about the performance benefits if your data is in a SQL db...if you're running a no-SQL db then you can specify only the fields you want but I'm guessing a SQL db is likely to lock and read the entire row/record, even if it's only returning a subset of values.
If you're building a big, public-facing API or if people will only want a small subset of available data fields exposed by the API, GraphQL makes sense, but I feel like devs are swapping out easier-to-implement and use REST APIs just because GraphQL sounds cool.
1: update a field in your model.
1b: the views that depend on exactly that data are automatically rerendered.
And I have to say, I'm not so fan of this peppy-feel-good-emoji way of writing I see more and more often. Especially if it's just hiding lack of substance.
I planned on using both at first when I started migrating to graphql, but quickly saw that indeed redux was becoming redundant when we use apollo. Especially since they added local state management [1].
[1]: https://www.apollographql.com/docs/react/essentials/local-st...
I guess one of the point the author was trying to make is that using GraphQL makes it easier to handle the state on the server, and it has multiple advantages (what if you fill a form half-way through on your desktop and want to resume on another device).
GraphQL also makes Http requests "cheaper", you only ask for what you need and reduce payloads in general.
You'd struggle to replicate most of that stuff in javascript even if you had days/weeks to do it. You'd be reinventing off the shelf components along the way that were completely bog standard 25 years ago; hand crafting event handling mechanisms; dealing with hard layout and positioning issues; etc.
That style of development seems to have died out for no good reason that I can think of other than that Javascript has been the only thing that actually works in a browser for the last 20 years. There's no other good reason for people to tolerate that.
Looking forward to WASM fixing that problem over the next few years. I might even get back to doing some frontend stuff.
On the other hand, the rise of cross-browser and cross-OS compatibility is really the root cause that drove a lot of this complexity from jQuery to React to even WASM, so you can't throw the baby out with the bathwater.
So because of that, there seems to be this mad scramble to standardise the tools needed to cope with all the different categories of change.
Managing events in a large UI can often be a nightmare, no matter how you approach it. Race condition errors are common, so the idea, for example, of a universal event bus, is a longstanding pattern for dealing with the complexity found in handling a large number of disparate events, and displaying their results.
Redux is the latest 'solution' to that problem, though I personally think that Backbone Radio (https://marionettejs.com/docs/master/backbone.radio.html) handled it best. Even the name suggests a solid understanding of the problem.
What's nice about Redux, is that by demanding this explicit contract between events and data changes, you're much less likely to experience race condition errors. But it's resultantly complicated. I'm not knowledgeable enough (or possibly too lazy right now) to tell you if the reducer pattern is the best way to guarantee immutability, but it works in Redux, despite the hurdles (read as 'complicated API').
GraphQL is nice, and does have it's place. Especially in making it much easier to decide exactly how server-side data is consumed by the client, so it's worth the time in some cases, sure.
But GraphQL should not be seen as a way to bypass the problems associated with managing events on the front end. Because that will eventually lead to server load issues, which are potentially far more expensive to fix.
This is the temptation when starting with Redux/Mobx/GraphQL/etc. You can't simply relegate reasoning through your application architecture to a single library or because some big company solved a problem with it.
Redux is a fantastic way to organise some of your app state & GraphQL is also great at a lot of stuff! Absolute no need to play the all-your-eggs-in-one-basket game.
I'm already using Firebase, most of my operations are already unique API calls. I typically dispatch an update to my store and to firebase simultaneously in one thunk action. I typically dispatch the thunk directly from a connected component or from it's connected parent container. Seems like GraphQL solves literally no problems for me
This way I have all the advantages of Redux, but with very terse BS-free code.