🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Step-by-Step Guide: Parent/Child Components and Lifting State

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

In React, components often need to share information. To do this, we use parent/child relationships and sometimes need to “lift state” so components can communicate effectively. Here’s a simple guide to get you started.






Summary




  1. Parent-to-Child Communication: Pass data using props.

  2. Child-to-Parent Communication: Use functions passed as props to update the parent’s state.

  3. Lifting State: Move state to the nearest common parent to share it between sibling components.






1. Parent and Child Components



Think of a parent component and child components quite literally as a parent and their children. The parent manages the house and passes chores down to the children.



Let’s create two components: Parent and Child. The parent will pass data to the child using props.



Parent Component:




CODE
import React from 'react';
import Child from './Child';

function Parent() {
const message = "Hello from Parent!";

return (
<div>
<h1>Parent Component</h1>
<Child message={message} />
</div>
);
};

export default Parent;







What do you think is the prop we will pass down to the Child?



Child Component:




CODE
import React from 'react';

function Child({ message }) {
return (
<div>
<h2>Child Component</h2>
<p>{message}</p>
</div>
);
};

export default Child;






When you render Parent, the child will display:



Hello from Parent!





2. Lifting State Up



Sometimes, a child component needs to send data back to its parent. This is where we “lift state.”



Here’s an example where the child sends a message to the parent:



Parent Component:




CODE
import React, { useState } from 'react';
import Child from './Child';

function Parent() {
const [childMessage, setChildMessage] = useState("");

const handleMessage = (message) => {
setChildMessage(message);
};

return (
<div>
<h1>Parent Component</h1>
<p>Message from Child: {childMessage}</p>
<Child onSendMessage={handleMessage} />
</div>
);
};

export default Parent;






Child Component:

What props do you think we will be passing down to the Child function?




CODE

import React from 'react';

function Child({ onSendMessage }) {
const sendMessageToParent = () => {
onSendMessage("Hello from Child!");
};

return (
<div>
<h2>Child Component</h2>
<button onClick={sendMessageToParent}>Send Message to Parent</button>
</div>
);
};

export default Child;






Now, when you click the button in the child component, the parent updates its state and displays:



Message from Child: Hello from Child!





3. Why Lift State?




  • If two child components need to share data, the parent can manage the state and pass it to both children.

  • It avoids redundant state logic across components.



Example: Sibling Communication




CODE
import React, { useState } from 'react';
import ChildOne from './ChildOne';
import ChildTwo from './ChildTwo';

function Parent () {
const [sharedState, setSharedState] = useState("");

return (
<div>
<h1>Parent Component</h1>
<ChildOne setSharedState={setSharedState} />
<ChildTwo sharedState={sharedState} />
</div>
);
};

export default Parent;






ChildOne Component:




CODE
import React from 'react';

function ChildOne ({ setSharedState }) {
return (
<div>
<h2>Child One</h2>
<button onClick={() => setSharedState("Message from Child One")}>
Send to Child Two
</button>
</div>
);
};

export default ChildOne;






ChildTwo Component:




CODE
import React from 'react';

function ChildTwo ({ sharedState }) {
return (
<div>
<h2>Child Two</h2>
<p>{sharedState}</p>
</div>
);
};

export default ChildTwo;






When ChildOne sends a message, ChildTwo automatically receives it via the parent.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Step-by-Step Guide: Parent/Child Components and Lifting State

Thematisch verwandte Begriffe: StepbyStep, Guide, ParentChild, Components · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...