✅ What does it mean?
In React, you can pass a function (method) from a parent component to a child component as a prop.
This allows the child component to communicate back with the parent or trigger some action in the parent.
🧠 Why use it?
React follows one-way data flow (parent → child).
But if a child needs to notify the parent (like "button was clicked" or "form was submitted"), you pass a method as a prop to do that.
🔹 Key Points
A method is just a JavaScript function.
You pass it to the child component like any other prop.
In the child, you call it using
props.methodName()(orthis.props.methodName()if it’s a class component).
👩🏫 Real-world Example
Imagine a parent component has a counter, and a child component has a button.
The child can’t directly update the parent’s state.
So the parent passes a method like
handleIncrementto the child.The child calls that method when its button is clicked — and the parent handles the logic.
<div class="infographic-propsvscode" id="pv">
<style>
.infographic-propsvscode {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-deep: #1942a8;
--accent-soft: #e3ebfd;
--line: #ddd6c9;
--radius: 8px;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', system-ui, sans-serif;
--mono-font: 'IBM Plex Mono', monospace;
max-width: 880px;
margin: 0 auto;
box-sizing: border-box;
background: transparent;
color: var(--ink);
font-family: var(--body-font);
}
.infographic-propsvscode[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #13171f;
--paper-alt: #1c212b;
--accent: #5b8def;
--accent-deep: #8fb4ff;
--accent-soft: #20293f;
--line: #2c313d;
}
.infographic-propsvscode * { box-sizing: border-box; min-width: 0; }
.pv-eyebrow {
font-family: var(--mono-font); font-size: 0.72rem; letter-spacing: 0.1em;
text-transform: uppercase; color: var(--accent); margin: 0 0 0.45rem;
display: flex; align-items: center; justify-content: center; gap: 0.4rem;
}
.pv-eyebrow::before { content: ""; width: 14px; height: 2px; background: var(--accent); display: inline-block; }
.pv-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4vw, 2.15rem); text-align: center;
margin: 0 0 0.5rem; line-height: 1.16; letter-spacing: -0.01em;
}
.pv-sub { color: var(--ink-soft); font-size: 0.95rem; text-align: center; margin: 0 0 1.7rem; }
/* ===== VS Code window — fixed dark theme regardless of page mode ===== */
.pv-window { border-radius: 8px; overflow: hidden; box-shadow: 0 16px 40px -14px rgba(0,0,0,0.45); border: 1px solid #1e1e1e; font-family: var(--mono-font); }
.pv-titlebar { background: #323233; display: flex; align-items: center; padding: 0.5rem 0.8rem; position: relative; }
.pv-dots { display: flex; gap: 0.4rem; }
.pv-dots span { width: 10px; height: 10px; border-radius: 50%; display: block; }
.pv-dots span:nth-child(1) { background: #ff5f56; }
.pv-dots span:nth-child(2) { background: #ffbd2e; }
.pv-dots span:nth-child(3) { background: #27c93f; }
.pv-titlebar-text { position: absolute; left: 50%; transform: translateX(-50%); font-size: 0.72rem; color: #cccccc; }
.pv-body { display: flex; height: 320px; }
.pv-resize-h { width: 5px; cursor: col-resize; background: transparent; flex-shrink: 0; transition: background 0.15s; touch-action: none; }
.pv-resize-h:hover, .pv-resize-h.is-dragging { background: #0078d4; }
.pv-resize-v { height: 5px; cursor: row-resize; background: transparent; flex-shrink: 0; transition: background 0.15s; touch-action: none; }
.pv-resize-v:hover, .pv-resize-v.is-dragging { background: #0078d4; }
.pv-window-resize {
height: 9px; background: #252526; cursor: row-resize; display: flex; align-items: center;
justify-content: center; border-top: 1px solid #1e1e1e; touch-action: none;
}
.pv-window-resize span { width: 30px; height: 3px; border-radius: 2px; background: #4a4a52; }
.pv-window-resize:hover span, .pv-window-resize.is-dragging span { background: #0078d4; }
.pv-toggle-btn {
background: none; border: none; color: #9a9a9a; cursor: pointer; display: flex; align-items: center;
padding: 0.3rem; border-radius: 3px; flex-shrink: 0;
}
.pv-toggle-btn:hover { background: #2a2d2e; color: #fff; }
.pv-toggle-btn svg { width: 13px; height: 13px; }
.pv-sidebar.is-hidden { width: 0 !important; overflow: hidden; border-right: none; }
.pv-terminal.is-collapsed { height: auto !important; }
.pv-terminal.is-collapsed .pv-term-body { display: none; }
.pv-terminal.is-collapsed .pv-term-toggle svg { transform: rotate(-90deg); }
.pv-activitybar { width: 40px; background: #333333; display: flex; flex-direction: column; align-items: center; padding-top: 0.7rem; gap: 1.1rem; flex-shrink: 0; }
.pv-activitybar svg { width: 18px; height: 18px; opacity: 0.55; }
.pv-activitybar svg.is-on { opacity: 1; }
.pv-sidebar { width: 175px; background: #252526; flex-shrink: 0; overflow-y: auto; border-right: 1px solid #1e1e1e; }
.pv-sidebar-head { font-size: 0.65rem; font-weight: 700; letter-spacing: 0.06em; color: #bbbbbb; padding: 0.6rem 0.8rem 0.3rem; }
.pv-sidebar-root { font-size: 0.74rem; color: #cccccc; padding: 0.2rem 0.7rem; display: flex; align-items: center; gap: 0.3rem; }
.pv-sidebar-folder { font-size: 0.74rem; color: #cccccc; padding: 0.2rem 0.7rem 0.2rem 1.4rem; display: flex; align-items: center; gap: 0.3rem; }
.pv-row {
display: flex; align-items: center; gap: 0.35rem; width: 100%; background: none; border: none;
color: #cccccc; font: inherit; font-size: 0.72rem; padding: 0.24rem 0.7rem 0.24rem 2.1rem;
cursor: pointer; text-align: left; border-left: 2px solid transparent;
}
.pv-row:hover { background: #2a2d2e; }
.pv-row.is-selected { background: #37373d; border-left-color: #0078d4; }
.pv-badge { width: 15px; height: 15px; border-radius: 3px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 0.44rem; font-weight: 700; color: #1a1a1a; background: #29b6f6; }
.pv-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.pv-tabbar { display: flex; background: #252526; border-bottom: 1px solid #1e1e1e; overflow-x: auto; }
.pv-tab {
display: flex; align-items: center; gap: 0.35rem; background: #1e1e1e; color: #808080; font: inherit;
font-size: 0.7rem; padding: 0.45rem 0.75rem; border: none; border-right: 1px solid #1e1e1e;
border-top: 2px solid transparent; cursor: pointer; flex-shrink: 0;
}
.pv-tab.is-active { color: #d4d4d4; border-top-color: #0078d4; background: #1e1e1e; }
.pv-tab .pv-badge { width: 13px; height: 13px; font-size: 0.4rem; }
.pv-code-wrap { flex: 1; background: #1e1e1e; overflow: auto; padding: 0.85rem 1rem; }
.pv-code-wrap pre { margin: 0; }
.pv-code-wrap code { font-family: var(--mono-font); font-size: 0.7rem; line-height: 1.7; color: #d4d4d8; white-space: pre; }
.tok-kw { color: #c678dd; }
.tok-tag { color: #e06c75; }
.tok-type { color: #61afef; }
.tok-attr { color: #d19a66; }
.tok-str { color: #98c379; }
.tok-punc { color: #6b7280; }
.tok-comment { color: #7f848e; font-style: italic; }
/* ===== terminal ===== */
.pv-terminal { background: #181818; border-top: 1px solid #2c2c2c; display: flex; flex-direction: column; height: 130px; flex-shrink: 0; }
.pv-term-head { display: flex; align-items: center; gap: 0.4rem; font-size: 0.6rem; letter-spacing: 0.05em; color: #9a9a9a; padding: 0.35rem 0.8rem; border-bottom: 1px solid #262626; text-transform: uppercase; }
.pv-term-body { flex: 1; overflow-y: auto; padding: 0.5rem 0.9rem; font-size: 0.7rem; line-height: 1.6; }
.pv-term-line { color: #d4d4d4; white-space: pre; opacity: 0; transform: translateY(2px); animation: pv-line-in 0.25s ease forwards; }
@keyframes pv-line-in { to { opacity: 1; transform: none; } }
.pv-term-line.is-ok { color: #7ee787; font-weight: 600; }
.pv-term-prompt { color: #7ee787; }
.pv-run-row { display: flex; align-items: center; gap: 0.5rem; margin-top: 0.2rem; }
.pv-run-btn {
font: inherit; font-size: 0.66rem; font-weight: 600; color: #1a1a1a; background: #7ee787;
border: none; border-radius: 4px; padding: 0.28rem 0.65rem; cursor: pointer; display: flex; align-items: center; gap: 0.3rem;
}
.pv-run-btn:disabled { opacity: 0.5; cursor: default; }
.pv-run-btn svg { width: 9px; height: 9px; }
.pv-run-hint { color: #6b7280; font-size: 0.66rem; }
@media (max-width: 620px) { .pv-sidebar { width: 130px; } }
@media (max-width: 480px) { .pv-activitybar { display: none; } }
/* ===== browser output ===== */
.pv-output-label {
font-family: var(--mono-font); font-size: 0.68rem; letter-spacing: 0.1em; text-transform: uppercase;
color: var(--ink-soft); text-align: center; margin: 1.5rem 0 0.7rem;
}
.pv-browser {
border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; max-width: 400px; margin: 0 auto;
box-shadow: 0 10px 24px -12px rgba(26,26,26,0.18);
opacity: 0; transform: translateY(8px); transition: opacity 0.4s ease, transform 0.4s ease; pointer-events: none;
}
.pv-browser.is-shown { opacity: 1; transform: none; pointer-events: auto; }
.pv-browser-bar { background: #e9e9e9; padding: 0.5rem 0.7rem; display: flex; align-items: center; gap: 0.5rem; }
.pv-browser-bar .pv-dots span { width: 8px; height: 8px; }
.pv-browser-url { font-family: var(--mono-font); font-size: 0.65rem; color: #555; background: #fff; border-radius: 4px; padding: 0.2rem 0.5rem; flex: 1; }
.pv-browser-page { background: #fff; padding: 1.5rem 1.3rem; font-family: Times, "Times New Roman", serif; color: #000; }
.pv-browser-page h1 { font-size: 1.15rem; margin: 0 0 0.7rem; }
.pv-browser-page h2 { font-size: 1rem; margin: 0 0 0.4rem; }
.pv-browser-page h3 { font-size: 0.9rem; margin: 0.7rem 0 0.4rem; }
.pv-browser-page p { margin: 0 0 0.5rem; }
.pv-browser-page button {
font-family: var(--body-font); font-size: 0.82rem; font-weight: 600; background: #efefef;
border: 2px outset #ccc; padding: 0.4rem 0.8rem; cursor: pointer;
}
.pv-browser-page button:active { border-style: inset; }
.pv-output-placeholder { text-align: center; font-size: 0.82rem; color: var(--ink-soft); }
.pv-takeaway {
margin-top: 1.7rem; padding: 0.95rem 1.2rem; background: var(--accent-soft); border-left: 3px solid var(--accent);
border-radius: 4px; font-size: 0.9rem; color: var(--ink); line-height: 1.5; max-width: 620px; margin-left: auto; margin-right: auto;
}
.pv-takeaway b { color: var(--accent-deep); }
.pv-takeaway code { font-family: var(--mono-font); }
@media (prefers-reduced-motion: reduce) {
.infographic-propsvscode *, .infographic-propsvscode *::before, .infographic-propsvscode *::after {
transition-duration: 0.001ms !important; animation-duration: 0.001ms !important;
}
}
</style>
<div class="pv-eyebrow">Methods as Props</div>
<h1 class="pv-title">Passing a Function Down as a Prop</h1>
<p class="pv-sub">Open the files, run the dev server, then click the button that's actually inside Child.</p>
<div class="pv-window">
<div class="pv-titlebar">
<div class="pv-dots"><span></span><span></span><span></span></div>
<div class="pv-titlebar-text">my-app — Visual Studio Code</div>
</div>
<div class="pv-body">
<div class="pv-activitybar">
<svg class="is-on" viewBox="0 0 24 24" fill="none" aria-hidden="true"><rect x="3" y="4" width="7" height="7" rx="1" stroke="#fff" stroke-width="1.6"/><rect x="14" y="4" width="7" height="7" rx="1" stroke="#fff" stroke-width="1.6"/><rect x="3" y="15" width="7" height="7" rx="1" stroke="#fff" stroke-width="1.6"/><rect x="14" y="15" width="7" height="7" rx="1" stroke="#fff" stroke-width="1.6"/></svg>
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true"><circle cx="10" cy="10" r="6.5" stroke="#fff" stroke-width="1.6"/><line x1="15" y1="15" x2="20" y2="20" stroke="#fff" stroke-width="1.6" stroke-linecap="round"/></svg>
</div>
<div class="pv-sidebar">
<div class="pv-sidebar-head">EXPLORER</div>
<div class="pv-sidebar-root">▾ MY-APP</div>
<div class="pv-sidebar-folder">▾ src</div>
<button class="pv-row is-selected" data-file="app" type="button"><span class="pv-badge">JS</span><span>App.js</span></button>
<button class="pv-row" data-file="parent" type="button"><span class="pv-badge">JS</span><span>Parent.jsx</span></button>
<button class="pv-row" data-file="child" type="button"><span class="pv-badge">JS</span><span>Child.jsx</span></button>
</div>
<div class="pv-resize-h" id="pvSidebarHandle" title="Drag to resize sidebar"></div>
<div class="pv-main">
<div class="pv-tabbar">
<button class="pv-toggle-btn" id="pvToggleSidebar" type="button" aria-label="Toggle sidebar" title="Toggle sidebar">
<svg viewBox="0 0 16 16" fill="none" aria-hidden="true"><rect x="1.5" y="2" width="13" height="12" rx="1.5" stroke="currentColor" stroke-width="1.3"/><line x1="6" y1="2" x2="6" y2="14" stroke="currentColor" stroke-width="1.3"/></svg>
</button>
<button class="pv-tab is-active" data-file="app" type="button"><span class="pv-badge">JS</span><span>App.js</span></button>
<button class="pv-tab" data-file="parent" type="button"><span class="pv-badge">JS</span><span>Parent.jsx</span></button>
<button class="pv-tab" data-file="child" type="button"><span class="pv-badge">JS</span><span>Child.jsx</span></button>
</div>
<div class="pv-code-wrap">
<pre><code id="pvCode"></code></pre>
</div>
<div class="pv-resize-v" id="pvTerminalHandle" title="Drag to resize terminal"></div>
<div class="pv-terminal">
<div class="pv-term-head">
<button class="pv-toggle-btn pv-term-toggle" id="pvToggleTerminal" type="button" aria-label="Toggle terminal" title="Toggle terminal">
<svg viewBox="0 0 16 16" fill="none" aria-hidden="true"><path d="M4 6l4 4 4-4" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<span>Terminal</span>
</div>
<div class="pv-term-body">
<div id="pvTermLines"><div class="pv-term-line" style="animation-delay:0s;"><span class="pv-term-prompt">$</span> </div></div>
<div class="pv-run-row">
<button class="pv-run-btn" id="pvRunBtn" type="button">
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M2.5 1.5v9l7-4.5z" fill="currentColor"/></svg>
<span id="pvRunLabel">npm start</span>
</button>
<span class="pv-run-hint" id="pvRunHint">runs the dev server</span>
</div>
</div>
</div>
</div>
</div>
<div class="pv-window-resize" id="pvWindowHandle" title="Drag to resize window"><span></span></div>
</div>
<div class="pv-output-label">Output</div>
<div id="pvOutputArea">
<p class="pv-output-placeholder">Run <code style="font-family:var(--mono-font)">npm start</code> above to see it here.</p>
<div class="pv-browser" id="pvBrowser">
<div class="pv-browser-bar">
<div class="pv-dots"><span></span><span></span><span></span></div>
<div class="pv-browser-url">localhost:3000</div>
</div>
<div class="pv-browser-page">
<h1>React Methods as Props Example</h1>
<h2>Parent Component</h2>
<p>Count: <span id="pvLiveCount">0</span></p>
<h3>Child Component</h3>
<button id="pvLiveBtn" type="button">Increment from Child</button>
</div>
</div>
</div>
<div class="pv-takeaway"><b>props.onButtonClick</b> is really Parent's own <code>handleIncrement</code> — when Child calls it, it's calling Parent's function, so Parent's state (and the count you see) updates, even though the click happened inside Child.</div>
</div>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Fraunces:wght@500;600&family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<script>
(function () {
var root = document.getElementById('pv');
if (!root) return;
/* --- dark mode detection: body attribute > cookie > postMessage > light --- */
function applyDark(dark) {
if (dark) root.setAttribute('data-theme', 'dark');
else root.removeAttribute('data-theme');
}
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') dark = true;
else if (bodyAttr === 'false') dark = false;
else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
applyDark(dark);
window.addEventListener('message', function (e) {
if (e.data && e.data.type === 'darkMode') applyDark(!!e.data.value);
});
/* --- file switching --- */
var files = {
app:
'<span class="tok-kw">import</span> <span class="tok-type">React</span> <span class="tok-kw">from</span> <span class="tok-str">"react"</span><span class="tok-punc">;</span>\n' +
'<span class="tok-kw">import</span> <span class="tok-type">Parent</span> <span class="tok-kw">from</span> <span class="tok-str">"./Parent"</span><span class="tok-punc">;</span>\n' +
'<span class="tok-kw">function</span> <span class="tok-type">App</span><span class="tok-punc">() {</span>\n' +
' <span class="tok-kw">return</span> <span class="tok-punc">(</span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">div</span> <span class="tok-attr">className</span><span class="tok-punc">=</span><span class="tok-str">"App"</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">h1</span><span class="tok-punc">></span>React Methods as Props Example<span class="tok-punc"></</span><span class="tok-tag">h1</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-type">Parent</span> <span class="tok-punc">/></span>\n' +
' <span class="tok-punc"></</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc">);</span>\n' +
'<span class="tok-punc">}</span>\n' +
'<span class="tok-kw">export</span> <span class="tok-kw">default</span> <span class="tok-type">App</span><span class="tok-punc">;</span>',
parent:
'<span class="tok-kw">import</span> <span class="tok-type">React</span><span class="tok-punc">, {</span> <span class="tok-type">useState</span> <span class="tok-punc">}</span> <span class="tok-kw">from</span> <span class="tok-str">"react"</span><span class="tok-punc">;</span>\n' +
'<span class="tok-kw">import</span> <span class="tok-type">Child</span> <span class="tok-kw">from</span> <span class="tok-str">"./Child"</span><span class="tok-punc">;</span>\n' +
'<span class="tok-kw">function</span> <span class="tok-type">Parent</span><span class="tok-punc">() {</span>\n' +
' <span class="tok-kw">const</span> <span class="tok-punc">[</span>count<span class="tok-punc">,</span> setCount<span class="tok-punc">]</span> <span class="tok-punc">=</span> <span class="tok-type">useState</span><span class="tok-punc">(</span>0<span class="tok-punc">);</span>\n' +
' <span class="tok-comment">// Method to update the counter</span>\n' +
' <span class="tok-kw">const</span> handleIncrement <span class="tok-punc">=</span> <span class="tok-punc">() => {</span>\n' +
' setCount<span class="tok-punc">(</span>count <span class="tok-punc">+</span> 1<span class="tok-punc">);</span>\n' +
' <span class="tok-punc">};</span>\n' +
' <span class="tok-kw">return</span> <span class="tok-punc">(</span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">h2</span><span class="tok-punc">></span>Parent Component<span class="tok-punc"></</span><span class="tok-tag">h2</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">p</span><span class="tok-punc">></span>Count: <span class="tok-punc">{</span>count<span class="tok-punc">}</</span><span class="tok-tag">p</span><span class="tok-punc">></span>\n' +
' <span class="tok-comment">{/* Passing method as a prop to child */}</span>\n' +
' <span class="tok-punc"><</span><span class="tok-type">Child</span> <span class="tok-attr">onButtonClick</span><span class="tok-punc">={</span>handleIncrement<span class="tok-punc">} /></span>\n' +
' <span class="tok-punc"></</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc">);</span>\n' +
'<span class="tok-punc">}</span>\n' +
'<span class="tok-kw">export</span> <span class="tok-kw">default</span> <span class="tok-type">Parent</span><span class="tok-punc">;</span>',
child:
'<span class="tok-kw">import</span> <span class="tok-type">React</span> <span class="tok-kw">from</span> <span class="tok-str">"react"</span><span class="tok-punc">;</span>\n' +
'<span class="tok-kw">function</span> <span class="tok-type">Child</span><span class="tok-punc">(</span>props<span class="tok-punc">) {</span>\n' +
' <span class="tok-kw">return</span> <span class="tok-punc">(</span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">h3</span><span class="tok-punc">></span>Child Component<span class="tok-punc"></</span><span class="tok-tag">h3</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"><</span><span class="tok-tag">button</span> <span class="tok-attr">onClick</span><span class="tok-punc">={</span>props.onButtonClick<span class="tok-punc">}></span>Increment from Child<span class="tok-punc"></</span><span class="tok-tag">button</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc"></</span><span class="tok-tag">div</span><span class="tok-punc">></span>\n' +
' <span class="tok-punc">);</span>\n' +
'<span class="tok-punc">}</span>\n' +
'<span class="tok-kw">export</span> <span class="tok-kw">default</span> <span class="tok-type">Child</span><span class="tok-punc">;</span>'
};
var codeEl = root.querySelector('#pvCode');
var rows = root.querySelectorAll('.pv-row');
var tabs = root.querySelectorAll('.pv-tab');
function selectFile(id) {
codeEl.innerHTML = files[id];
rows.forEach(function (r) { r.classList.toggle('is-selected', r.getAttribute('data-file') === id); });
tabs.forEach(function (t) { t.classList.toggle('is-active', t.getAttribute('data-file') === id); });
}
rows.forEach(function (r) { r.addEventListener('click', function () { selectFile(r.getAttribute('data-file')); }); });
tabs.forEach(function (t) { t.addEventListener('click', function () { selectFile(t.getAttribute('data-file')); }); });
selectFile('app');
/* --- terminal run simulation --- */
var termLines = root.querySelector('#pvTermLines');
var runBtn = root.querySelector('#pvRunBtn');
var runLabel = root.querySelector('#pvRunLabel');
var runHint = root.querySelector('#pvRunHint');
var browser = root.querySelector('#pvBrowser');
var placeholder = root.querySelector('.pv-output-placeholder');
var running = false;
var lines = [
{ text: '$ npm start', cls: 'is-cmd', delay: 0 },
{ text: 'Starting the development server\u2026', cls: '', delay: 500 },
{ text: 'Compiled successfully!', cls: 'is-ok', delay: 1350 },
{ text: 'You can now view my-app in the browser.', cls: '', delay: 1600 },
{ text: ' Local: http://localhost:3000', cls: '', delay: 1850 }
];
runBtn.addEventListener('click', function () {
if (running) return;
running = true;
runBtn.disabled = true;
runLabel.textContent = 'running\u2026';
browser.classList.remove('is-shown');
placeholder.style.display = 'block';
termLines.innerHTML = '';
lines.forEach(function (line) {
setTimeout(function () {
var div = document.createElement('div');
div.className = 'pv-term-line ' + line.cls;
div.textContent = line.text;
termLines.appendChild(div);
}, line.delay);
});
setTimeout(function () {
placeholder.style.display = 'none';
browser.classList.add('is-shown');
runBtn.disabled = false;
runLabel.textContent = 'npm start';
runHint.textContent = 'server running \u2014 rerun anytime';
running = false;
}, 2300);
});
/* --- live counter: Child's button really calls Parent's handler --- */
var liveCount = 0;
var liveCountEl = root.querySelector('#pvLiveCount');
var liveBtn = root.querySelector('#pvLiveBtn');
liveBtn.addEventListener('click', function () {
liveCount = liveCount + 1;
liveCountEl.textContent = liveCount;
});
/* --- resize handles (pointer-capture drag) --- */
function makeResizable(handle, opts) {
var dragging = false, startPos = 0, startSize = 0;
handle.addEventListener('pointerdown', function (e) {
dragging = true;
handle.classList.add('is-dragging');
startPos = opts.axis === 'x' ? e.clientX : e.clientY;
startSize = opts.target.getBoundingClientRect()[opts.axis === 'x' ? 'width' : 'height'];
handle.setPointerCapture(e.pointerId);
e.preventDefault();
});
handle.addEventListener('pointermove', function (e) {
if (!dragging) return;
var pos = opts.axis === 'x' ? e.clientX : e.clientY;
var delta = pos - startPos;
if (opts.invert) delta = -delta;
var size = Math.min(opts.max, Math.max(opts.min, startSize + delta));
opts.target.style[opts.prop] = size + 'px';
if (opts.onResize) opts.onResize(size);
});
function stop() { dragging = false; handle.classList.remove('is-dragging'); }
handle.addEventListener('pointerup', stop);
handle.addEventListener('pointercancel', stop);
}
var sidebar = root.querySelector('.pv-sidebar');
var sidebarHandle = root.querySelector('#pvSidebarHandle');
var lastSidebarWidth = sidebar.getBoundingClientRect().width || 175;
makeResizable(sidebarHandle, {
axis: 'x', target: sidebar, prop: 'width', min: 100, max: 280, invert: false,
onResize: function (s) { lastSidebarWidth = s; }
});
var terminal = root.querySelector('.pv-terminal');
var terminalHandle = root.querySelector('#pvTerminalHandle');
var lastTerminalHeight = terminal.getBoundingClientRect().height || 130;
makeResizable(terminalHandle, {
axis: 'y', target: terminal, prop: 'height', min: 60, max: 280, invert: true,
onResize: function (s) { lastTerminalHeight = s; }
});
var pvBody = root.querySelector('.pv-body');
var windowHandle = root.querySelector('#pvWindowHandle');
makeResizable(windowHandle, {
axis: 'y', target: pvBody, prop: 'height', min: 320, max: 640, invert: false
});
/* --- show/hide toggles --- */
var toggleSidebarBtn = root.querySelector('#pvToggleSidebar');
var sidebarHidden = false;
toggleSidebarBtn.addEventListener('click', function () {
sidebarHidden = !sidebarHidden;
if (sidebarHidden) {
var w = sidebar.getBoundingClientRect().width;
if (w > 0) lastSidebarWidth = w;
sidebar.classList.add('is-hidden');
sidebarHandle.style.display = 'none';
} else {
sidebar.classList.remove('is-hidden');
sidebar.style.width = lastSidebarWidth + 'px';
sidebarHandle.style.display = '';
}
});
var toggleTerminalBtn = root.querySelector('#pvToggleTerminal');
var terminalCollapsed = false;
toggleTerminalBtn.addEventListener('click', function () {
terminalCollapsed = !terminalCollapsed;
if (terminalCollapsed) {
var h = terminal.getBoundingClientRect().height;
if (h > 30) lastTerminalHeight = h;
terminal.classList.add('is-collapsed');
terminalHandle.style.display = 'none';
} else {
terminal.classList.remove('is-collapsed');
terminal.style.height = lastTerminalHeight + 'px';
terminalHandle.style.display = '';
}
});
})();
</script>🔹 Parent.jsx
What happens here:
The parent component uses
useStateto create a state variable calledcountwith an initial value of0.It defines a method
handleIncrement()that increases the count by 1 usingsetCount().It renders:
A heading and the current value of
countThe
Childcomponent, and passes the methodhandleIncrementto it as a prop namedonButtonClick.
✅ Key idea: The parent owns the state and the method to change it. The child just calls it.
🔹 Child.jsx
What happens here:
This is a simple functional component that receives
props.It renders:
A heading
A button that calls
props.onButtonClickwhen clicked
✅ Key idea: The child doesn't know anything about the count. It just executes a method sent from the parent.
🔁 Flow of Execution (when user clicks button):
You click the button in the Child component.
props.onButtonClick()is called — this actually refers to thehandleIncrement()function from the Parent.handleIncrement()runs in the parent and updates the state usingsetCount().The parent re-renders, and the updated
countis shown.