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..
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.
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!!
I usually extract any data-handling logic from React components into separate functions, and then wrap them in hooks when needed. If you rewrite the components (perhaps even in a different framework), these parts of the code won't have to change
TanStack, let it handle the state of data calls, errors, retries, loading state, etc.
Seriously, adding this during a React migration cut down like 60% of the hand-coded network logic our website was doing and found several issues with the old logic.
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.
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.
That's basically hooks with memo, but provided by React by default with components being memoized based on the props. It's always a nice pattern to separate logic in HOC and presentation in the wrapped component.
Separating logic (and fetching) from rendering is something I'd certainly like to see more of in React - and just putting it in a hook isn't enough because that still has the rendering component in control of everything.
5
u/Dubstephiroth Jul 05 '26
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 Jul 05 '26
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/effnd Jul 05 '26
I usually extract any data-handling logic from React components into separate functions, and then wrap them in hooks when needed. If you rewrite the components (perhaps even in a different framework), these parts of the code won't have to change
2
u/valbaca Jul 05 '26
TanStack, let it handle the state of data calls, errors, retries, loading state, etc.
Seriously, adding this during a React migration cut down like 60% of the hand-coded network logic our website was doing and found several issues with the old logic.
2
u/Character-Blood3482 Jul 05 '26
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.
2
u/dustofdeath Jul 06 '26
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 Jul 06 '26
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/azhder Jul 05 '26
If you understand only S from SOLID, you can already write a lot cleaner code.
14
u/[deleted] Jul 05 '26
[removed] — view removed comment