Progressive React
houssein.me
houssein.me
Never mind the fact that we're plowing the depths of functional programming, complex data structures and asynchronous wizardry just to make sure that displaying a few bytes of text and flat-shaded borders isn't too slow.
I don't remember GUI programming being that hard in the years before this. At least for these rather simple results. Getting a full DTP platform out on a 20 mhz machine with 4 MB of RAM required some advanced pattern wizardry, but most web UI patterns aren't that complicated. They just aren't helped by the client-server nature and Javascript being Javascript, as much as you wrap it up in the Emperor's new C# clothes.
> Sites have become bigger, more interactive and more complex and this number is still gradually increasing in an upward trend year after year.
Yeah, but is it useful complexity? The whole frontend scene often reeks of "bullshit job" syndrome. To get results customers could live without in a framework not re ally suited for it, we're developing complex tools, even more complex tools to package and deploy them and then have more coaches, trainers, video walkthrough creators and premature optimization providers than ever before.
FireFox Quantum on Windows 10 with a pretty badass CPU. Also slow in Opera. Also slow in Vivaldi.
And it crashed Chrome Canary.
For a web-tech post, this is pretty fucking bad.
I also tried scrolling a number of different ways - with autoscrolling, mousewheel, page up/down and arrow keys.
All of the machines I tried it on are 4-5 years old except for my iPhone which came out in 2017.
It works great.
What changed everything for me was adding GraphQL and getting the majority of my API communication and data storage out of redux. Now, sagas and redux are further down the list of tools I reach for, complexity is way down across the board, and the sagas and reducers that’s remain are more narrowly scoped and easier to reason about and maintain.
Agreed. There's a simplicity to React / Redux that I absolutely adore. There's very little magic going on: things happen because you've explicitly told them to. Side-effects in Redux seem to throw this out the window.
I've used sagas in a couple apps that truly did have very complex async workflows, and they were great for that use case. But yeah, using sagas _just_ for something like data fetching is definitely overkill.
[0] https://redux.js.org/faq/actions#what-async-middleware-shoul...
[1] https://redux.js.org/style-guide/style-guide/#use-thunks-for...
I haven't tried saga, just rxjs with redux which was definitely good. I've also worked with Elm, which supposedly inspired redux. Elm does it perfectly, I don't know why Redux missed async.
I’m super happy that you’re here saying that. I’d go farther than “most apps” to “almost no apps” but, though I’ve mostly worked with apps where I wish they hadn’t used sagas, I have a lot of admiration for the saga concept and library.
But they all lead to things I would consider "hacky". Since it's all side effects, making sure two things that are "side effects" of the one action run in consistent order can be troublesome, since it's usually down to "well which one registered with the middleware first?"
And, if you ever need to actually simply `await` an action, you need to set up some middleware that will give you a promise that resolves / rejects when certain actions happen. Then you need to tie metadata to your actions if it stands that multiple of the action you're waiting to resolve happen.
Or, some people recommend creating a promise before dispatching, passing that promise reference around, checking for it in your sagas / observable.
All things I would consider super hacky.
I've been using my own middleware for years that simply lets you make `payload` a function, async function, or promise, and it's all incredibly more stable when things get complicated, and newcomers can grok whats going on in a second. You get `/start`, `/success`, `/error` actions baked in. And since it's promise-based, you can `await dispatch()` when you need to. If you need to get the state, or dispatch more actions, you can, right from the original action.
For times when you truly do want something to be a side effect (read: incredibly rarely because my actions have access to `dispatch` and `getState`), I use a very simple middleware like redux-saga but with standard async / await syntax.
I've been intrigued by people praising these side-effect workflows for some time, but after using redux-observable, I don't think I'll ever go near them again.
Apologies for the rant.
Im not blaming redux for this, this is clearly a problem with how we implemented redux. But our team of 3 devs would be much much better off by having used Mobx.
I've seen better implementations of Redux and https://github.com/isubasinghe/advanced-redux-patterns is one of them (this is code by Nir Kaufman for his advanced redux patterns talk)
Out of curiosity, what issues have you been seeing with using thunks?
I'd specifically encourage you to check out our new official Redux Toolkit package. It includes utilities to simplify several common Redux use cases, including store setup, defining reducers, immutable update logic, and even creating entire "slices" of state at once:
You might also want to look at the new "Style Guide" page we've added to the docs, which lists our recommended patterns and practices for using Redux:
I've heard this before but don't really understand it. Most of my reducers are storing and maybe updating data loaded from the server. What's wrong with this pattern?
[0] https://redux.js.org/style-guide/style-guide/#model-actions-...
[1] https://github.com/dmmulroy/talks/blob/master/event-driven-r...
[2] https://youtu.be/K6OlKeQRCzo?t=2626 / https://rangle.slides.com/yazanalaboudi/deck#/
I don't think actions are what they're talking about, but reducers as setters vs reducers with more complex logic. I stumbled against the same thing last week, and rather than duplicating the logic in two reducers I settled on putting the logic in the action and turning both reducers into simple setters.
When you mentally model an action as a "setter", like `SET_PIZZAS_ORDERED`, the reducer usually has almost no logic and just blindly accepts whatever value was in the action. The work of calculating the value was done before the action was dispatched.
If you model actions conceptually as "events", the corollary is that the work of calculating the new state typically ends up in the reducer.
Issues with thunks are maybe(?) due to our implementation again, but we have way too many dispatches being called (100s of lines of dispatch calls for one action sometimes). It becomes quite hard to trace what happens when a certain business logic is executed because of this.
For example, say we have an action to delete a domain level entity, lets say this is a book, well books can have paragraphs, paragraphs can have sentences and sentences can have words (normalized state, terrible example I know, but all I could think of atm). Well the action to delete a book, has dispatches to delete all children entities as well. These dispatches actually may have other dispatches as well for ui stuff etc. Doing redux this way really didn't scale well for us, but I suppose we have very very unique, complex business logic compared to other projects.
The style guide looks excellent, I think this would have made the codebase easier to follow. I will defs be following this for my personal projects.
Thanks
https://news.ycombinator.com/item?id=21853160
Also, note that our new Redux Toolkit package will at least help simplify your existing logic, and you can begin migrating to it incrementally:
API shapes change and evolve, and mapping raw API shapes to components (through Redux) became a major PITA, real fast.
reselect solved all of that; we transform almost all data to a “view layer friendly” format before passing it into the components, keeping them as dumb as possible. We also moved most “view business logic” to the selector level: The check might involve user permissions, subscription plan, etc, etc, but the component only receives ‘shouldThisThingBeDisplayed‘. API shape/changes very rarely impact the components.
This approach makes it much easier to test in our experience. Business logic is tested without any concern for the components themselves. Visuals are covered by Storybook and ChromaticQA.
Jokes aside, everything has its place. You're still allowed to hate it though. For example, I hate Redux because everyone uses it for every React app and it's got boilerplate up the wazoo.
Just know that if you "hate" things, though, you won't learn as much in life, like I won't learn how to use time travel debugging. Almost anything that gets this popular has at least a few core ideas we should probably learn, or learn from.
The way to reduce the boilerplate is to use a mutable paradigm, but then you lose the simplifications that the immutable paradigm gives you.
We recommend using Immer as the best way to write immutable logic with Redux [1], and our new Redux Toolkit package [2] automatically uses Immer internally to let you write reducers like this:
const reducer = createReducer(initialState, {
updateItem(state, action) {
state.first.second[action.payload.id].fourth = action.payload.value
}
})
[0] https://immerjs.github.io/immer/[1] https://redux.js.org/style-guide/style-guide/#use-immer-for-...
The time saved in both authoring and maintenance pays for itself in learning the standards.
I only said "at least a few core ideas" -- not, "at least a few unique core ideas". Sure there are other places you can learn the same things, which of course doesn't mean that JS is now somehow stripped of that value.
Admittedly this works best on projects that don't require a massively complex interactive front-end, like news sites[0] and data reporting tools. But at some point you can just choose to start hydrating the interactive components in the browser without changing your back-end setup at all.
[0]: https://wildlyinaccurate.com/introducing-a-faster-bbc-news-f...
Really everything is delivering HTML ... and maybe something else.
I was thinking in the simplest form as the person I was responding to seemed to be saying that just to do something ultra basic react was not something they liked.
There's ways to do a lot of things (I swear that is the real sticking point on HN when it comes to react discussions).
But just serving up straight HTML, you could actually use react, but you don't have to do it "just" to do that.
If you're stuck in a spot where you HAVE to use it, that's the fault of whomever decided that, not react.
I'm using chrome on Android and it kills my browser every time.
This comments section tho.
As people exited Rails, the cost of maintenance has gone up... since you have to pay a lot to get someone writing Rails now.
Rails is definitely more niche but it's still a great framework IMHO and it's not dead. Still gets feature updates to this day.
React's ecosystem, on the other hand, has embraced strong typing (and alongside it, tooling that lets you drill deep into any method you see on the screen), and it's fully compatible with teams working on discrete backend and frontend components. It's not likely to disappear any time soon, the same way jQuery hasn't disappeared. Yes, it adds a performance cost, but so long as Zawinski's Law holds [0], functional state-to-display-object transformations will continue to be the paradigm, and React continues to lead the pack there. And if new display object abstractions come into play (say, React Native in-browser rendering widgets on the GPU directly via webasm or new browser-specific APIs), React can and will adapt.
Just my 2c.
[0] https://medium.com/programming-philosophy/zawinskis-law-2090...