r/javascript • u/chuttadi2007 • 17d ago
AskJS [AskJS] What small JavaScript pattern made your React code cleaner?
[removed]
4
u/Dubstephiroth 17d ago
Notes and very rough documentation has made it easier visualise the flow of data through the project... and writing dumb components with no logic at all..
3
u/sylvant_ph 17d ago
I ain't sure if my examples match precisely your description, but once in a while I feel the need and benefit of using IIFE(Immediately Invoked Function Expressions - had to google the abbreviation) and currying. The first can scope down a more complex condition a bit, and the second can allow some sort of slight dependency injection. With IIFE you can both keep the logic inline, yet have it split down into readable segments, instead of forcing it into complex ternary operators.
2
u/catladywitch 13d ago
And then they're easy to extract (maybe wrap them into a custom hook if they touch state) if you need to reuse them. That's a good one, I'm taking note!!
2
u/Character-Blood3482 17d ago
Not the js's or react's specifically thing but I like to return { data, error } for any functions that including try-catch. So that who ever use that function can if/else on the error to process futher more.
1
u/catladywitch 13d ago
as functional as js and react have become over the years, I really wish it was easy to move to Result-based apis for error management instead of the glorified call/ccs that try/catch blocks provide, but it'd require such a massive rewrite of everything i guess monadic handling of stuff beyond Promises and kinda useEffect is gonna remain a Rust/Scala/OCaml/Haskell/F# thing, plus pattern matching in JavaScript has been on the ECMAScript list for ages anyway
2
u/dustofdeath 16d ago
Regardless of the framework - i would say it's the separation of business logic/API and rendering that improves messy code.
2nd would be reduction of if-if else-else spam. Reordering function structure can eliminate most of these with the use of returns.
2
u/ndev42 16d ago
The biggest one for me was stopping treating components as the place where everything happens. Once I moved everything else out (API calls into their own layer, business logic into plain functions or hooks) the components basically became declarative and boring to read. Which is exactly what you want!
The other thing that sounds dumb but genuinely helped was just making more components. If you've got a conditional block rendering a chunk of UI, that's probably its own component. Smaller components means each one has less reason to be complicated in the first place.
3
u/Verzuchter 17d ago
Probably the most profound impact was, moving to angular because it's more popular here in Europe. I now develop frontends like I develop backends and it has made shifting so much easier.
Felt like a world has been opened to me.
1
u/eindbaas 17d ago
Components are about orchestrating the ui, meaning a lot of logic and business logic does not belong in a component.
1
u/lookarious 16d ago
Pull out logic from React components to anything that scales properly, for us it was MVVM (MobX)
1
14
u/LessAd8002 17d ago
I started pulling out all the API calls into a separate file with named functions and it cleaned up components so much. Before that my useEffect blocks were getting ridiculous with all the fetch logic just sitting there
Also early returns in components instead of nesting conditions everywhere, that one took me way too long to figure out