Lifting State Up, Refs and React DevTools: Put State Where It Is Needed
A task board where the filter is stuck in one component, fixed by lifting it to App. Then useRef as a box React does not watch (three clicks, zero renders), a ref that refocuses an input, and the tree React DevTools shows.
What you will learn
- Spot when two sibling components need the same value
- Lift state to the closest common parent: value down, change up
- Use useRef for values that should not cause a render, and for page elements
- Check props, hooks and renders with React DevTools
Before you start
- React props and function props
- useState and how a setter triggers a render
When two components need the same value, where should that value live? This article answers it with a small task board: three filter buttons (all, open, done), a card that lists the tasks, and a box to add a new one. In code that is TaskFilter, TaskList and AddTask, all inside App.
The filter bar knows which filter is picked. The list needs the same filter to decide which tasks to show. Let's see where the filter has to live, then meet refs, and check everything in React DevTools.
Two siblings, one value
The obvious first try keeps the filter in the component that changes it:
import { useState } from 'react'
import { start, type Filter } from '../tasks'
import TaskList from '../TaskList'
// BEFORE: the filter state is stuck in TaskFilter
function TaskFilter() {
const [filter, setFilter] = useState<Filter>('all')
console.log(`render: TaskFilter (${filter})`)
return (
<button onClick={() => setFilter('done')}>
done
</button>
)
}
export default function StuckBoard() {
console.log('render: StuckBoard')
return (
<div className="board">
<TaskFilter />
<TaskList tasks={start} filter="all" />
</div>
)
}
renders: StuckBoard, TaskFilter (all), TaskList
list: 5 of 5 shown
click: done
renders: TaskFilter (done)
list: 5 of 5 shown
Clicking done changes TaskFilter's own state, and only TaskFilter renders again. The list still says 5 of 5, because on line 21 it gets a fixed filter="all". It simply can't see the filter. And TaskFilter can't hand the value across: the two are siblings, and props only flow down from a parent to its children.
Lift the state up
The fix is to move the state to the closest parent both components can see. Here that is App:
import { useState } from 'react'
import { start, type Filter } from './tasks'
import TaskFilter from './TaskFilter'
import TaskList from './TaskList'
import AddTask from './AddTask'
export default function App() {
// shared state: in the closest common parent
const [filter, setFilter] = useState<Filter>('all')
const [tasks, setTasks] = useState(start)
console.log('render: App')
function addTask(title: string) {
const id = tasks.length + 1
setTasks([...tasks, { id, title, done: false }])
}
return (
<div className="board">
<TaskFilter value={filter}
onChange={setFilter} />
<TaskList tasks={tasks} filter={filter} />
<AddTask onAdd={addTask} />
</div>
)
}
Line 9 is the same useState, moved up. From App the value goes down: TaskList gets filter on line 22 and only reads it. TaskFilter gets two props on lines 20 and 21: value, the current filter, and onChange, which is setFilter itself.
A filter with no state
import type { Filter } from './tasks'
type Props = {
value: Filter
onChange: (next: Filter) => void
}
const FILTERS: Filter[] = ['all', 'open', 'done']
// no state here: the parent owns the value
function TaskFilter({ value, onChange }: Props) {
console.log('render: TaskFilter')
return (
<div className="filters">
{FILTERS.map((f) => (
<button key={f}
className={f === value ? 'on' : ''}
onClick={() => onChange(f)}>
{f}
</button>
))}
</div>
)
}
export default TaskFilter
There is no useState here. The button whose name equals value gets the class on. A click calls onChange(f), which asks App and changes nothing locally. Forget to pass onChange in App and TypeScript stops the build:
TS2741: Property 'onChange' is missing in type '{ value: Filter; }' but required in type 'Props'.
A list that reads the filter
// does this task pass the filter?
export function matches(t: Task, filter: Filter) {
if (filter === 'all') return true
return filter === 'done' ? t.done : !t.done
}
type Props = { tasks: Task[]; filter: Filter }
// reads the filter, never changes it
function TaskList({ tasks, filter }: Props) {
console.log('render: TaskList')
const shown = tasks.filter((t) =>
matches(t, filter))
return (
<div className="card">
<p>{shown.length} of {tasks.length} shown</p>
<ul>
{shown.map((t) => (
<li key={t.id}>
{t.done ? '✔' : '○'} {t.title}
</li>
))}
</ul>
</div>
)
}
TaskList works out shown from its props on every render. With the filter set to done, Write report and Book flights pass, so it prints 2 of 5 shown.
Follow one click: up, then down
renders: App, TaskFilter, TaskList, AddTask
list: 5 of 5 shown
click: done
renders: App, TaskFilter, TaskList, AddTask
list: 2 of 5 shown
rows: ✔ Write report | ✔ Book flights
Did AddTask need to render? Not really, nothing changed for it. But when a parent renders, every component inside it renders too. Keep that in mind for the DevTools part below.
A ref: a box React does not watch
import { useRef, useState } from 'react'
// counts in state: every click asks for a render
export function StateClicks() {
const [clicks, setClicks] = useState(0)
console.log('render: StateClicks')
return (
<button onClick={() => setClicks(clicks + 1)}>
state: {clicks}
</button>
)
}
// counts in a ref: React does not watch the box
export function RefClicks() {
const clicks = useRef(0)
console.log('render: RefClicks')
function add() {
clicks.current += 1
console.log('ref clicks =', clicks.current)
}
return (
<button onClick={add}>
ref: {clicks.current}
</button>
)
}
useRef(0) gives a small object, { current: 0 }, and returns the same box on every render. React does not watch it. Click the ref button three times, then let a parent render it again:
renders: RefClicks
ref clicks = 1
ref clicks = 2
ref clicks = 3
renders: none
page: ref: 0
renders: RefClicks
page: ref: 3
Three clicks, zero renders, and the page still says 0. The box holds 3, but nobody asked React to draw again. When a parent renders it, the new render reads the box and shows 3. Now the state button, clicked three times:
renders: StateClicks
click x3
renders: StateClicks, StateClicks, StateClicks
page: state: 3
Each click asks for a render, so state renders four times in total and shows 3. The rule that comes out of this: if the screen shows it, keep it in state. If only your code needs it, such as a timer id or a page element, a ref keeps it without a render.
A ref to an element
import { useRef } from 'react'
type Props = { onAdd: (title: string) => void }
function AddTask({ onAdd }: Props) {
// a ref to the <input> element itself
const inputRef = useRef<HTMLInputElement>(null)
console.log('render: AddTask')
function add() {
const input = inputRef.current!
if (!input.value.trim()) return
onAdd(input.value.trim())
input.value = ''
input.focus() // cursor back in the box
}
return (
<div className="add">
<input ref={inputRef} placeholder="New task" />
<button onClick={add}>add</button>
</div>
)
}
On the first render there is no input yet, so the box holds null. Line 19 attaches the ref, and after drawing React puts the real input element in inputRef.current. Type "Review budget" and click add:
renders: App, TaskFilter, TaskList, AddTask
type: Review budget
click: add
renders: App, TaskFilter, TaskList, AddTask
list: 6 of 6 shown
focus: <input>, value ""
The value goes up to App through onAdd (6 of 6 shown), the box is cleared, and focus() puts the cursor back for the next task.
Check it in React DevTools
React DevTools is a browser extension. Its Components tab shows the tree React keeps, with each component's props and hooks. The lecture prints the same tree with a small script after one click on done:
App
hooks State: "done" | State: Array(5)
TaskFilter
props value: "done" | onChange: Æ’ bound dispatchSetState
TaskList
props tasks: Array(5) | filter: "done"
AddTask
props onAdd: Æ’ addTask
hooks Ref: {current: <input>}
The two State hooks live in App: the filter and the five tasks. TaskFilter has props only, and onChange shows as bound dispatchSetState, React's name for App's setter. TaskList reads tasks and filter. AddTask has onAdd plus one Ref hook holding the input.
In the DevTools settings, switch on "Highlight updates when components render". Click done and DevTools frames App, TaskFilter, TaskList and AddTask, the same four as in the render log. Click the ref button and nothing is framed, because nothing rendered.
Recap
| Idea | What to remember |
|---|---|
| Siblings | Can't pass props sideways |
| Lifting state up | Move it to the closest common parent; pass the value and the setter down |
| Data and events | Data goes down as props, changes go up through callbacks |
useRef | Same box every render; changing .current causes no render |
| Element ref | ref={inputRef} lets your code call focus() |
| DevTools | Components tab for props and hooks; highlight updates for renders |
Try it yourself: add an "open" count above the list that reads the same lifted tasks state, and check in DevTools which components frame when you add a task. Next: forms and actions in React 19.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
React State, Events, Lists and Conditional Rendering: Follow One Click
Follow one click through a React counter, render by render: what useState hands back, why a plain variable never updates the page, ternaries and &&, map with keys, and the index-as-key bug that moves a typed note to the wrong task.
React useEffect: When It Runs, What Cleanup Is For, and When You Don't Need One
Watch useEffect on a timeline with real render counts: the dependency array deciding re-runs, a timer cleanup that keeps one timer alive, the leak StrictMode exposes, and two effects you should delete (a copied total and a click flag).
Forms and Actions in React 19: Controlled Inputs, Form Actions, useActionState and useFormStatus
Four versions of an add-task form, traced with real logs: a controlled input, a classic onSubmit, a form action that gets FormData and resets itself, and useActionState with a pending button from useFormStatus, plus the draft trick that keeps typed text after an error.
React Context and Custom Hooks: Share One Value, Reuse the Logic
Replace three hops of prop drilling with a context, see the nearest Provider win, count which components re-render on a theme toggle, then pull localStorage logic into a useLocalStorage hook and break the rules of hooks on purpose.
