Skip to main content

7 React Patterns That Made Me Code Smarter, Not Harder

 


It’s 4 AM. I’m knee-deep in spaghetti code. My useEffect dependencies are screaming, the UI flickers like a cheap horror movie, and some mystery re-render is haunting my app every time I breathe.

Classic React chaos. Ever been there?

I was a decent developer. But React humbled me. Hard.

Turns out, knowing useState And slapping on some JSX doesn’t mean you know what the hell you’re doing. It means you started. But to ship maintainable, scalable apps? You need patterns. Real ones.

Here are 7 React patterns that slapped sense into me — and might just save your sanity too.


Edited by me

1. The “Component as Function, Not Dumpster” Pattern

You know what I’m talking about.

That one component that does everything. Fetches data, renders UI, handles logic, scrolls the page, makes your coffee…

Stop. Please.

A component should do one thing well, not ten things poorly.

The Fix: Extract. Abstract. Repeat.

// Bad
function UserProfile() {
const [data, setData] = useState(null);
useEffect(() => {
fetchUser().then(setData);
}, []);
return <div>{data?.name}</div>;
}

// Better
function useUser() {
const [data, setData] = useState(null);
useEffect(() => {
fetchUser().then(setData);
}, []);
return data;
}
function UserProfile() {
const user = useUser();
return <div>{user?.name}</div>;
}

Separation of concerns. It’s not just for backend nerds.

2. The “Hooks Are Just Functions” Realization

Remember when you first saw useEffectYou thought, "Wow, this is elegant."

Then you added three useEffects, two useStates, and now it’s bug soup.

Let’s be clear:

Hooks are just functions. You can write your own. And you should.

Custom hooks are the cheat code of React.

function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return width;
}

Boom. Reusable. Clean. Understandable.

If you’re not writing custom hooks, you’re leaving Legos all over the floor.

3. The “Lift State, But Don’t Launch It Into Orbit” Principle

Yes, we’ve all heard it:

“Lift state up!”

Cool. But there’s a line.

If your parent component starts looking like a NASA control center, you’ve lifted too hard.

Keep the state close to where it’s needed. Only lift when necessary.

You don’t need a global state to control a dropdown. I promise.

4. The “Composition Over Configuration” Gospel

Tired of prop drilling like you’re digging for oil? Welcome to the club.

React’s secret sauce is composition. Use it.

function Modal({ children }) {
return <div className="modal">{children}</div>;
}
<Modal>
<h2>Are you sure?</h2>
<button>Yes</button>
<button>No</button>
</Modal>

Clean. Flexible. No props gymnastics.

Your component should be a LEGO brick, not a LEGO Death Star.

5. The “Render Props Aren’t Dead” Hot Take

Everyone acts like render props died when hooks came around.

They didn’t. They’re just chilling in a corner, waiting for someone to remember how powerful they are.

function MouseTracker({ render }) {
const [pos, setPos] = useState({ x: 0, y: 0 });
useEffect(() => {
const handleMouseMove = (e) => setPos({ x: e.clientX, y: e.clientY });
window.addEventListener("mousemove", handleMouseMove);
return () => window.removeEventListener("mousemove", handleMouseMove);
}, []);
return render(pos);
}
// Use it like this
<MouseTracker render={({ x, y }) => <p>Mouse at {x}, {y}</p>} />

Don’t throw away tools just because they’re not trendy.

6. The “Context Is for Settings, Not Your Whole App” Rant

I swear, if I see one more app using Context for everything, I’m switching to Vue out of spite.

Context is great for themes, auth, language — things that rarely change.

But if you’re using it to pass around dynamic state that updates often?

Prepare for performance hell.

Use state management libs for that. Or better yet, local state + props. Yeah, old-school. Because it works.

7. The “UseReducer for Complex State” Revelation

When your state looks like this:

const [state, setState] = useState({
step: 1,
error: null,
loading: false,
data: null,
});

It’s time.

useReducer isn’t just for Redux nerds. It’s for people who like control.

function reducer(state, action) {
switch (action.type) {
case "NEXT_STEP":
return { ...state, step: state.step + 1 };
case "SET_ERROR":
return { ...state, error: action.payload };
default:
return state;
}
}

You’ll feel like a wizard. Or at least, someone who doesn’t want to cry reading their code.

Finally, Code Like You’ll Maintain It Hungover

These patterns didn’t just make me a better React developer.

They made me less angry, more productive, and way more confident in my codebase.

React’s flexibility is a gift and a curse. You can write beautiful abstractions… or create a flaming trash pile.

Use patterns. Write for future-you. And please — don’t nest ternaries.

Got opinions? Good. Drop a comment, leave a clap, or argue with me in public.

I love a good React debate.

Just don’t tell me class components are better — we’re not doing that again.

Comments

Popular posts from this blog

CSS only Click-handlers You Might not be using, but you should

  You’re building a simple website, a good-looking landing page with a “See More” button. Instinctively, you reach for JavaScript to handle the button click event. But wait — what if I told you that CSS alone could do the job? Yes. CSS is often underestimated, but it can handle click interactions without JavaScript. In this guide, you’ll learn how to create CSS-only click handlers using the :target pseudo-class, and explore scenarios where this approach makes perfect sense. The :target Pseudo-Class CSS offers several pseudo-classes that let you style elements based on different states ( :hover , :focus , :checked ). But there’s one you might not have used before —  :target . The :target pseudo-class applies styles to an element when its ID matches the fragment identifier in the URL (the part after # ). This behavior is commonly seen when clicking an anchor link that jumps to a section on the same page. Here’s a simple example : <a href="#contact">Go to Contact</...

The 10 Best New CSS Features in 2025 Already Supported in All Major Browsers

  CSS keeps evolving with new capabilities that make our work faster, cleaner, and more powerful. Thanks to the latest browser advances (Baseline 2024), many fresh features now work across all major engines. Below are ten highlights you can start using right away. Do you want more? Let’s check out my project, CSSToday: csstoday.dev/ 1. Scrollbar-Gutter & Scrollbar-Color When a browser displays a scrollbar, the layout can shift as space is taken up. With scrollbar-gutter , you can preserve scrollbar space even before scrolling begins: .scrollable {   scrollbar-gutter : stable both-edges; } You can also style your scrollbars with scrollbar-color : .scrollable {   scrollbar-color : #444 #ccc ; } This ensures a consistent look and prevents layout jumps. What it’s good for ✅ scrollbar-gutter keeps layouts stable by reserving space for a scrollbar, preventing annoying shifts when the scrollbar appears. scrollbar-color lets you style the scrollbar’s track and thumb, en...

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...