6 Code Smells In React I Look Out For

If you've got any questions feel free to DM on Twitter @reillyjodonnell
Search for a command to run...

If you've got any questions feel free to DM on Twitter @reillyjodonnell
No comments yet. Be the first to comment.
Someone shared this image above. At first glance, I wasn’t sure about the perf implications — so let’s break it down from first principles :D Let’s deconstruct this monster Here’s the code: // Function to flatten React Context Providers. const flatte...

Intro Hello it’s me from the future! I originally was going to go over file based routing but pivoted to going over important SSR concepts with React / how Bun makes it easy. It’s full of struggles with hydration (mismatches), React entry points, and...

history? Serverless is EVERYWHERE and for good reason: nearly infinite scalability, physically closer to users, and pay-for-what-you-use pricing. But not without tradeoffs — vendor lock-in, (potentially) higher costs, and the infamous cold-starts. cf...

Powered by bash and AppleScript!

Sockets can only transmit binary (text.) Imagine we have this data const message = {id: '123', name: 'Reilly', message: 'Hey'} Here's the problem: we want to send this data to the ui but we have hundreds of useEffects spread throughout the codebase....

I've recently undergone analyzing and improving a React application's performance and have noticed some pretty serious code smells that affect both code quality and performance.
Here are some code smells that I watch out for in a React application:
A massive component (500+ LOC) - I hesitate to write a specific number of LOC because there are always exceptions but 500 begins to become TOO large. Components should be small and practice Individual Responsibility.
Business logic and UI are intertwined - components should only contain the UI and not have complex business rules intertwined in the component. I suggest abstracting away business rules either into a helper function or into a custom hook.
No tests for business logic - each set of business logic rules should be tested to document the expected outcome. Tests add confidence to the software that other programmers can use to be certain that they didn't break something pre-existing. It is extremely helpful to have tests when you need to refactor to get immediate feedback through the testing framework.
Massive amounts of props are being passed - if there are a lot of props being passed through the component I check two things:
Can we compose this component better with composition (see smell 5)
If we're prop drilling certain props, like a user object or some international translation function that doesn't change that often, let's put it into Context.
Lack of use of composition - If a codebase or a section of code isn't taking advantage of composition it points to a lack of understanding of React. Using composition results in fewer unnecessary rerenders and less prop drilling.
Two fantastic resources if you're unfamiliar with composition: an article by Kent C. Dodds https://epicreact.dev/one-react-mistake-thats-slowing-you-down/ and a video by Michael Jackson (no not that one) https://www.youtube.com/watch?v=3XaXKiXtNjw&t=592s
Incorrect usage of hooks - the most commonly misused I've seen are useEffect - which should not be used to adjust state or props when a certain value changes, or replicate what componentDidMount/ other lifecycle methods did. Instead use it to sync an external system to React (network, DOM, etc.) These days with how awesome React-Query/SWR are you don't even need it.
Also, stop putting useCallbacks everywhere - premature optimization is the root of all evil.