Sub topic 4
Inspecting props, state, and hooks
This chapter assumes you can write function components, use useState and props, and open a browser's Elements and Console panels. You should also have React Developer Tools installed and a React app running in development mode.
What the side panel shows
When you select a component in the Components tab, the right-hand panel shows the data that component used in its latest render. It has these sections:
- props: the values the parent passed in.
- hooks: one row for each hook the component calls, in call order. Class components show state here instead.
- rendered by: the chain of parent components that created this one.
- source: the file and line where the component is defined, when the build includes that information.
The panel shows a snapshot from the last render. If the app changes while you look at it, the panel refreshes on the next render.
A component to inspect
import { useState, useRef, useMemo } from 'react';
function useCounter(start) {
const [count, setCount] = useState(start);
return { count, increment: () => setCount((c) => c + 1) };
}
export function Cart({ items, onCheckout }) {
const [coupon, setCoupon] = useState('');
const inputRef = useRef(null);
const total = useMemo(
() => items.reduce((sum, item) => sum + item.price, 0),
[items]
);
const { count, increment } = useCounter(0);
return (
<section>
<input
ref={inputRef}
value={coupon}
onChange={(e) => setCoupon(e.target.value)}
/>
<p>Total: {total}</p>
<button onClick={increment}>Clicked {count}</button>
<button onClick={() => onCheckout(total)}>Checkout</button>
</section>
);
}Select Cart and read the panel in this order.
Reading props
Props appear as name: value. Primitive values such as strings, numbers, and booleans show inline. Objects and arrays have a disclosure arrow, so you can expand items and check each entry. Functions show as ƒ, followed by the function's name. In the example, onCheckout appears as ƒ onCheckout(). An anonymous inline arrow function shows as ƒ ().
If a prop is undefined or has the wrong shape, the cause is in the parent. Use rendered by to move up one level and check what the parent passed.
Reading hooks
Hooks are listed in the order the component calls them. The panel labels each one by type:
| Hook in code | Label in the panel | Value shown |
|---|---|---|
| `useState` | State | Current value |
| `useRef` | Ref | The `current` value |
| `useMemo` | Memo | The cached result |
| `useCallback` | Callback | The cached function |
| `useReducer` | Reducer | Current state |
| Custom hook | Hook name, such as `Counter` | Nested rows for hooks inside it |
For Cart, you would see State: "", Ref: null (or an <input> element after mount), Memo: 0, then Counter. Expand Counter to find its own State: 0.
Common mistakes
- Reading the wrong component. Wrappers such as
memoandforwardRefappear as badges on the component, not as separate rows. Check that the name in the panel header is the component you meant. - Trusting a stale view. Values change only when React re-renders. If a ref changes without a render, the panel can lag behind. Click the component again to refresh.
- Missing names. Components defined as anonymous functions appear as
Anonymous. Give them a name so you can find them. - Matching hooks by name. Hooks are matched by position, not by variable name.
coupondoes not appear in the panel, onlyState: "". Count the rows against your code.
Exercise
In the Components tab, Cart shows Memo: 0 even though the app passed three items with prices. Which panel section do you check first, and why?
Show answer
Check props first. useMemo computes its value from items, so an empty or wrongly shaped items prop in the props section explains a total of 0. If items is correct, check whether the price fields are numbers. If the props are wrong, use "rendered by" to inspect the parent.