Profile
Back to NewsBack
Dev.to 6 min
Reader Mode
๐Ÿš€ Day 7 of Learning React: How Do Components Share Data? Understanding Lifting State Up

๐Ÿš€ Day 7 of Learning React: How Do Components Share Data? Understanding Lifting State Up

2 weeks ago

๐Ÿ“Œ Missed Day 6? I explored how React renders components, why immutable updates matter, how HMR works behind the scenes, and different approaches to handling forms. You can **read it here* and then come back. I'll wait. โ˜•๏ธ*


Yesterday, I spent most of my time learning how React updates the UI.

Today, I ran into a completely different problem.

I had a registration form.

I also had a list of user cards.

Whenever someone filled out the form and clicked Register, I wanted a new user card to appear automatically.

Sounds simple...

Except there was one problem.

The form and the user cards lived in different components.

That's when I learned one of React's most important patterns:

Lifting State Up.

Let's dive in. ๐Ÿš€


๐Ÿง  Quick Recap

Yesterday's big question was:

How does React know when something should be re-rendered?

Here's what I took away from Day 6:

  • ๐Ÿ›๏ธ map() helps render multiple components from data.
  • ๐Ÿ”„ React prefers immutable updates.
  • ๐Ÿ“ Forms become much cleaner when using a single reusable change handler.
  • ๐Ÿšซ DRY (Don't Repeat Yourself) makes React code easier to maintain.

Today we're answering a different question:

How do two components share the same data? ๐Ÿค”


๐Ÿ—๏ธ The Project

Today's project looked something like this.

App
โ”œโ”€โ”€ Register
โ””โ”€โ”€ UserCard

The Register component collected user information.

The UserCard component displayed all registered users.

Pretty straightforward.

At first, I thought these two components could somehow communicate with each other directly.

They can't.

That misunderstanding was the beginning of today's lesson.


๐Ÿค” The Problem

Inside my Register component, I already had state.

const [formData, setFormData] = useState({
  name: "",
  email: "",
  password: "",
  image: "",
});

Submitting the form updated formData perfectly.

But another question appeared.

How does UserCard know a new user was registered?

It doesn't.

Because UserCard has absolutely no access to Register's state.

That state belongs only to the Register component.


๐Ÿšซ Sibling Components Can't Read Each Other's State

This was probably the biggest realization of today's class.

Both components were children of the same parent.

App
โ”œโ”€โ”€ Register
โ””โ”€โ”€ UserCard

That makes them siblings.

And sibling components cannot directly access each other's state.

For a while, I kept thinking there must be some way for one component to "look inside" another.

There isn't.

React doesn't work like that.


๐Ÿ’ก The Solution Was Surprisingly Simple

Instead of trying to make the two components communicate...

I moved the shared state up.

From this:

Register
โ””โ”€โ”€ users state

To this:

App
โ”œโ”€โ”€ users state
โ”œโ”€โ”€ Register
โ””โ”€โ”€ UserCard

That small change solved everything.

And that's exactly why this pattern is called:

Lifting State Up.

We're simply moving shared state to the nearest common parent.


๐Ÿ“ฆ The Parent Owns the State

Instead of storing users inside Register, I stored them inside App.

const [users, setUsers] = useState([]);

Now App became the single source of truth.

Instead of owning the users...

The child components simply used them.


๐Ÿ“จ Props Started Making Much More Sense

Once the state lived inside App, everything connected beautifully.

Register received the updater function.

<Register setUsers={setUsers} />

Whenever someone submitted the form,

Register simply called:

setUsers((prev) => [...prev, formData]);

It wasn't updating its own state anymore.

It was asking App to update the shared state.

That tiny detail completely changed how I think about props.

They're not just for sending data.

They can also send functions.


๐Ÿช„ Where Did the New User Card Come From?

This was my favorite part.

I never manually created a new card.

Instead, App rendered the cards like this:

{
  users.map((elem) => (
    <UserCard
      key={elem.email}
      user={elem}
    />
  ));
}

As soon as users changed...

React rendered another UserCard.

No DOM manipulation.

No extra logic.

Just a state update.

That pattern is becoming more and more familiar every day.


๐Ÿ’ก My Mental Model

Here's how I visualize today's lesson.

User Fills Form
        โ†“
Register Calls setUsers()
        โ†“
App Updates users State
        โ†“
App Re-renders
        โ†“
Updated users Passed to UserCard
        โ†“
New User Card Appears โœจ

That flow made today's topic click for me.


๐Ÿค Why Lift the State?

At first, moving state to another component felt unnecessary.

But after today's project, it actually made perfect sense.

Now there is:

  • โœ… One place where the users are stored.
  • โœ… One source of truth.
  • โœ… Both components stay synchronized.
  • โœ… No duplicated state.

Instead of each component trying to manage the same information...

They simply rely on the parent.


๐Ÿ’ก My Biggest Takeaways Today

  • ๐Ÿ—๏ธ Components don't automatically share state.
  • ๐Ÿ‘จโ€๐Ÿ‘ง Sibling components can't directly access each other's state.
  • โฌ†๏ธ Shared state should live in the nearest common parent.
  • ๐Ÿ“จ Props can pass both data and functions.
  • ๐Ÿ”„ Updating the parent's state automatically updates every child that depends on it.

๐Ÿ“š Learning Source

I'm currently learning React through the React Cohort 3.0 by Devendra Dhote at Sheriyans Coding School.

This article isn't a copy of the course.

It's my personal understanding after today's class, rewritten entirely in my own words.

Writing these articles helps me reinforce what I've learned, and hopefully helps other beginners who are on the same journey. ๐Ÿค

If I've misunderstood something, I'd genuinely appreciate your corrections in the comments. ๐Ÿ˜Š


๐Ÿ™Œ Final Thoughts

Today's lesson wasn't about learning another Hook or another React API.

It was about learning where state actually belongs.

Earlier, I thought state should always stay inside the component that uses it.

Now I know that's not always true.

If multiple components need the same information, the best solution isn't to duplicate the state.

It's to move that state to their nearest common parent and let React's data flow do the rest.

That one idea made component communication feel much less mysterious.

See you on Day 8! ๐Ÿš€


๐Ÿ’ฌ When you first learned React, did you also try making sibling components share state directly, or did lifting state up click immediately for you? I'd love to hear your experience in the comments. ๐Ÿ˜Š

I'd love to hear your experience in the comments. ๐Ÿ˜Š

If you're following along with this series, you can also find me on GitHub, where I'll be sharing my projects and documenting my progress.

Bismay-exe (Bismay.exe) ยท GitHub

๐Ÿ‘‹ Hi, Iโ€™m Bismay ๐Ÿ’ป Developer passionate about building clean, minimal, and elegant apps ๐Ÿš€ Focused on coding ๐ŸŒฑ Always learning, creating & new ideas - Bismay-exe

favicon github.com

๐Ÿค– AI Disclosure: This article is based on my own React learning journey, class notes, code experiments, and understanding. I used ChatGPT to help improve the writing, structure, and readability of this post. I reviewed and verified the technical explanations before publishing, and I take responsibility for everything shared here.

Thanks for reading! ๐Ÿš€


Chat with me
Menu