DEV Community

Cover image for From Prop Drilling to Context API😏
Usama
Usama

Posted on

From Prop Drilling to Context API😏

Introduction – Why I Decided to Deep-Dive Into Code Flow

For the last few days, I wasn’t just coding — I was trying to understand the soul of a React project.

Instead of rushing to build new features, I picked an existing project from my course — an Atomic Blog / Post App — and made a different goal for myself:

“Today, I won’t build. Today, I will analyze.”

My purpose was simple but powerful:
I wanted to understand how data flows, how state is managed, and why certain architectural decisions are made. Not just what works, but why it works.

This deep-dive changed the way I see React. Especially when it comes to state management and Context API.


The Project Structure – Seeing the Big Picture

The project was a simple Post App, but its structure revealed a lot.

At the top sat the App component — the brain of the application.
It controlled two critical states:

  • posts → the list of all blog posts
  • searchQuery → the filter logic for searching posts

Every major action in the app revolved around these two pieces of state.
And this is where I started noticing something important…

All important logic lived at the top.
But the UI elements that needed that logic were buried deep below.


The Problem – The Prop Drilling Journey

This is where I encountered the famous React pain point: Prop Drilling.

To add a new post, the function handleAddPost was created in App.
But the form that actually needed it was three levels below.

The journey looked like this:

App → Main → FormAddPost
Enter fullscreen mode Exit fullscreen mode

Main wasn’t using the function.
It was simply acting as a messenger — passing the prop down without touching it.

At first, this seemed harmless.
But then I imagined…

What if this app had 10 nested components?
What if multiple functions needed to travel this path?

Suddenly, it didn’t look clean anymore.
It looked fragile, repetitive, and difficult to scale.

That was my first real “Aha!” moment.


The Logic – Understanding the Core Functions

Adding Posts – The Functional Update Pattern

The handleAddPost logic was elegant and clever.

Instead of directly modifying the state, it used the functional update pattern:

setPosts(posts => [newPost, ...posts])
Enter fullscreen mode Exit fullscreen mode

What this does:

  • Takes the previous state safely
  • Merges the new post at the top
  • Avoids stale state issues
  • Keeps updates predictable

This was a subtle but powerful lesson:
React state updates should be intentional, not accidental.


FormAddPost – Local Logic, Global Impact

Inside the FormAddPost component, the logic was simple but disciplined:

  • Check if title and body are not empty
  • If valid → call handleAddPost
  • Clear local state after submission

This separation of concerns was beautiful:

  • Local state for form inputs
  • Global state for posts list

It showed me how React encourages small, responsible components.


Search & Clear – Control from the Top

The Header component handled two user actions:

  • Search Filtering → updates searchQuery
  • Clear Posts → triggers handleClearPosts

Even though these buttons lived in the header UI,
their impact reached the entire application.

This reinforced one truth:

In React, who owns the state controls the behavior.


The Realization – Why Context API Became the Next Step

After tracing every function, every prop, and every state flow,
one thought kept repeating in my mind:

“There must be a cleaner way.”

And that’s when Context API clicked for me.

Context API isn’t magic.
It’s simply a way to say:

“Stop passing data through every middle component.
Let components access what they need directly.”

Instead of:

App → Main → FormAddPost
Enter fullscreen mode Exit fullscreen mode

We can create a Context Provider at the top,
and any child component can consume it instantly.

No messengers.
No unnecessary props.
No deep chains.

Just direct access.


Conclusion – Mastering State Flow Is the Real Skill

Before this analysis, I thought learning React meant:

  • Hooks
  • Components
  • Styling
  • Routing

Now I realize something deeper.

React mastery is about understanding data flow.

When you understand:

  • Where state lives
  • Who controls it
  • How it moves
  • Why it’s structured that way

You stop being a “React user”
and start becoming a React thinker.

This Post App wasn’t just a project.
It was my turning point toward Context API and cleaner architecture.

I didn’t just learn a feature.
I learned why the feature exists.

And that “why” is what transforms coding
from typing syntax…
into building systems with intention.

Top comments (1)

Collapse
 
bhavin-allinonetools profile image
Bhavin Sheth

This is a great breakdown. The moment when prop drilling looks fine… and then suddenly feels unscalable is something every React dev hits 😄
I like how you focused on why Context exists, not just how to use it — understanding data flow really is the difference between using React and thinking in React.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.