Sub topic 4
Inspecting props, state, and hooks
This chapter assumes you can write React function components, use useState and props, and use the browser's Elements and Console panels. It also assumes the React Developer Tools extension is installed and you have a React app running in development mode. The earlier chapters covered installing the extension and navigating the component tree. Here you will read what the right-hand pane of the Components tab tells you about a selected component.
The working model
Every component instance gets its input from two places. The parent passes in props. React stores the rest inside the component, as state and other hooks. When a bug shows wrong data on screen, the first question is which of the two sources is wrong. The Components tab answers it without console.log calls.
The diagram shows where each kind of data comes from.
When you select a component in the tree, the right pane shows the data React holds for that instance. Each instance has its own data. Three <Item> rows have three separate sets of props and hooks, so select the specific row you care about.
What the right pane contains
The pane has several sections. Which ones appear depends on the component.
| Section | What it shows | Notes |
|---|---|---|
| Props | The values the parent passed in, including `children`, `key`, and `ref` when present | Read-only data from the component's point of view |
| Hooks | One entry per hook call, in call order, for function components | Replaces the single State section used by class components |
| State | The `this.state` object of a class component | Does not appear for function components |
| Rendered by | The chain of components that created this element | Helps you find which parent is responsible for bad props |
| Source | The file and line where the component is defined | Covered with the Console tools in a later chapter |
Reading props
Props appear as a list of name: value rows. Primitives (strings, numbers, booleans, null, undefined) show inline. Objects and arrays show a collapsed summary such as {id: 4, name: "Ada"} or Array(3). Click the arrow to expand them. You can drill into nested data as deep as the object goes.
Functions appear as ƒ followed by the function name, for example ƒ handleSelect(). An inline arrow function with no name often shows only ƒ (). DevTools shows no function body in this row. That is enough to tell you a function was passed, but not which one. Use the source link or the Console to find out more.
React elements passed as children show up as a prop too, so you can check what a wrapper component received.
Typical prop errors
- Wrong shape. An API returned
{ data: [...] }and the component expects an array. The Props section showsitems: {data: Array(3)}where you expecteditems: Array(3). undefinedor missing prop. A prop name typo in the parent (onSelct) leaves the expected prop absent from the list, and the typo'd name appears instead.- Stringly typed values. The row shows
count: "3"in quotes, which is a string, instead ofcount: 3. - A new reference every time. The values look identical but the object is rebuilt on each render. You cannot see identity in the pane, but the Profiler and Highlight Updates features cover it in later chapters.
Reading hooks
Function components keep state in hooks, so the Hooks section matters most for modern code. Consider this component.
import { useReducer, useRef, useState } from 'react';
function useCounter(initial) {
const [count, setCount] = useState(initial);
return { count, increment: () => setCount((c) => c + 1) };
}
function reducer(state, action) {
switch (action.type) {
case 'next':
return { step: 'pay' };
default:
return state;
}
}
export function Cart({ user, items }) {
const [coupon, setCoupon] = useState('');
const [checkout, dispatch] = useReducer(reducer, { step: 'review' });
const inputRef = useRef(null);
const { count, increment } = useCounter(0);
return (
<section>
<h2>{user.name}'s cart ({items.length} items)</h2>
<input
ref={inputRef}
value={coupon}
onChange={(e) => setCoupon(e.target.value)}
/>
<button onClick={increment}>Clicked {count} times</button>
<button onClick={() => dispatch({ type: 'next' })}>
Step: {checkout.step}
</button>
</section>
);
}Select Cart and the Hooks section lists entries in the order the hooks were called:
State: ""forcoupon.Reducer: {step: "review"}forcheckout.Ref: {current: <input>}forinputRef.Counter, a group that contains its ownState: 0.
Several details help you read this list.
- Entries are labeled by hook type, not by your variable name. A component with five
useStatecalls shows five rows labeledState. Match them by position in your source. DevTools can try to load hook names from your source code. Look for the button near the Hooks heading. The names it finds come from parsing your code, so treat them as a guide. - Custom hooks become groups.
useCounterappears asCounter, with theuseprefix removed. The hooks inside it are nested under it. A custom hook that wraps severaluseStatecalls is therefore easy to isolate. - Values are the current committed values. The pane shows what React last rendered with. If you called
setCoupon('SAVE10')in an event handler, the pane still shows the old value until React re-renders. The pane updates when the render finishes. - Order matters. If a hook sits inside a condition, React's rules of hooks are broken. The Hooks list is a quick way to see that a component has a different number of entries between renders.
State in class components
A class component has no Hooks section. The pane instead shows a State section with the this.state object. Expand it as you would an object prop. The rest of the pane works the same way.
A worked debugging pass
Suppose the cart heading reads "undefined's cart (3 items)". Follow these steps.
- Select
Cartin the tree. If you do not know which element it is, use the select-element arrow in the DevTools toolbar and click the heading on the page. - Open the Props section and expand
user. - The row reads
user: {firstName: "Ada"}. The component readsuser.name, so the data has a different field name from the one the component expects. - Look at "rendered by" to find the parent that built
user. Select it and check its own props. You are walking up the tree until you find the component where the shape first goes wrong.
The same walk works for state. If a hook value is wrong, check whether the code that sets it receives bad props, or whether your update logic is wrong. The split between props and hooks tells you where to look next.
Common mistakes
- Inspecting the wrong component. Wrapper components such as
memo,forwardRef, and context providers add extra nodes. AMemobadge marks a wrapped component. Select the node that holds your props. - Looking for state in the wrong place. State set in a parent never appears in a child's Hooks section. It shows up in the child only as a prop.
- Trusting the tree after navigation. After a route change or a conditional render, the component you selected may unmount. The pane then clears or shows nothing. Select it again after the new render.
- Anonymous components. A component created as
export default () => ...has no name and shows asAnonymous. Give components a name so they are easy to find. - Expecting to see closures. The pane shows values, not the variables captured in a function. A stale closure bug (a handler using an old
count) is invisible here. You will see the currentcountwhile the handler used an older one.
Practice
Exercise
A Price component displays "NaN". In the Components tab you select it and see props amount: "12.50" and quantity: 2. Hooks shows one State: 0. Which section holds the likely cause, and what is the cause?
Show answer
The Props section. amount is the string "12.50" (shown in quotes), so multiplying it in a way that expects a number, or mixing it with other values, can produce NaN. The fix is to convert it with Number(...) where the parent passes it, or to correct the data source. The hook state of 0 is a valid number and is not the problem.
Exercise
You select a Cart component that uses three useState calls and a custom hook useCart. In the Hooks section, how do the entries appear, and how do you decide which State row belongs to which variable?
Show answer
The three useState calls appear as three rows labeled State, in the order they were called. The custom hook appears as a group named Cart, with the hooks it calls nested inside. To match a row to a variable, count the position of the call in the source, or use the load hook names option to have DevTools label the rows from your code.