Lift state to a common parent
Choose one owner for shared information
When two parts of an interface need the same changing value, store that value in a component that can supply both parts. For example, a workshop directory needs its search input and its result count to agree. The nearest common parent can own the search text, send it downward as a prop, and receive change requests from the input. This is called lifting state. Each App.jsx example in this chapter is independent and uses the Chapter 3 project setup; all visual results are predictions until browser verification.
import { useState } from 'react';
const workshops = ['Paper folding', 'Wood carving', 'Paper printing'];function matchingWorkshops(names, query) { const term = query.trim().toLowerCase(); return names.filter(name => name.toLowerCase().includes(term));}function SearchField({ query, onQueryChange }) { return <label>Find a workshop <input value={query} onChange={event => onQueryChange(event.target.value)} /></label>;}function Results({ names }) { return <section><h2>Workshops</h2><p>{names.length} matches</p> <ul>{names.map(name => <li key={name}>{name}</li>)}</ul></section>;}export default function App() { const [query, setQuery] = useState(''); return <main><h1>Workshop directory</h1> <SearchField query={query} onQueryChange={setQuery} /> <Results names={matchingWorkshops(workshops, query)} /> </main>;}Initially there are three matches. Typing paper requests a parent update, which provides the new query to SearchField and two matching names to Results. The list is computed from the current query, not separately stored. The child does not write into its props: it calls the supplied callback. Names are unique in this small fixture; use stable record IDs when names can repeat.
Keep independent state near its users
import { useState } from 'react';
function WorkshopCard({ title }) { const [showTip, setShowTip] = useState(false); return <section><h2>{title}</h2> <button type="button" aria-expanded={showTip} onClick={() => setShowTip(v => !v)}> {showTip ? 'Hide' : 'Show'} preparation for {title} </button>{showTip && <p>Bring a notebook.</p>} </section>;}export default function App() { return <main><h1>Workshop preparation</h1> <WorkshopCard title="Drawing" /><WorkshopCard title="Pottery" /> </main>;}Opening Drawing does not open Pottery. That is the desired requirement here, so local state is appropriate. Lifting all state to App creates extra coupling. A component may receive some values as props and manage others locally; ownership is a decision about individual values, not an all-or-nothing property of a component.
Let siblings request changes to the same value
import { useState } from 'react';
function SessionPicker({ session, onSelect }) { return <label>Session <select value={session} onChange={e => onSelect(e.target.value)}> <option value="morning">Morning</option><option value="afternoon">Afternoon</option> </select></label>;}function ResetSession({ onReset }) { return <button type="button" onClick={onReset}>Reset to morning</button>;}export default function App() { const [session, setSession] = useState('morning'); return <main><h1>Book a workshop</h1><SessionPicker session={session} onSelect={setSession} /> <p>Selected: {session}</p><ResetSession onReset={() => setSession('morning')} /> </main>;}Select Afternoon, then activate Reset to morning. Both the select and the summary return to morning because they read the same parent-owned value. If SessionPicker copied session into its own useState, later parent changes would not automatically replace that copy. Avoid duplicate writable representations unless they intentionally represent separate concepts, such as a saved value and an unsaved draft.
Practice
Add a clear-search button beside Results in the first example. Decide which component owns query and which callback should clear it. Predict the match count after searching for a nonexistent workshop and then clearing.