Sub topic 5
Editing values live to reproduce bugs
This chapter assumes you can write React function components, use useState and props, and open the Components tab of React Developer Tools. You should also know how to run your app in development mode.
Why edit values instead of code
Some bugs only appear in a specific state. Examples are an empty list, a very long name, or an error flag. Reaching that state through the UI can take many clicks, or it may depend on a server response you can't control.
The Components tab lets you change props, state, and hook values on a mounted component. React re-renders the component with the new value. You see the result at once, with no code change, no reload, and no lost state elsewhere in the app.
What you can edit
Select a component in the tree. The right-hand panel lists its props, state, and hooks. Primitive values (strings, numbers, booleans) are editable. Double-click a value, type the new one, and press Enter.
| Value type | How to edit |
|---|---|
| String, number | Double-click the value and type a new one |
| Boolean | Click the checkbox next to it |
| Object or array | Expand it and edit the individual entries |
| Function | Not editable |
Hook values appear under "hooks" in order, for example State: 0. Custom hooks are grouped under their own name, with the built-in hooks nested inside.
A worked example
This component shows a badge for unread messages. It has a bug: when count is large, the badge text overflows its container.
import { useState } from 'react';
export function Inbox({ title }) {
const [count, setCount] = useState(0);
return (
<section>
<h2>{title}</h2>
<span className="badge">{count}</span>
<button onClick={() => setCount(count + 1)}>New message</button>
</section>
);
}Reproducing the overflow by clicking would take a hundred clicks. Instead:
- Open the Components tab and select
Inbox. - Under hooks, find
State: 0. - Double-click
0, type99999, and press Enter. - Look at the page. The badge now shows
99999and you can see the overflow. - Open the Elements panel and adjust the CSS to confirm a fix, such as
max-widthwithtext-overflow: ellipsis.
You can also edit the title prop to a very long string and watch how the heading wraps.
Common errors
- The edit disappears. You edited a prop and the parent re-rendered. Edit the state in the parent instead, or find where the value originates.
- The value is not editable. Functions and some complex objects can't be changed in the panel. Edit the primitive fields inside them.
- Nothing changes on screen. The value you edited may not affect the output. Check that the component actually reads it.
- A bad value crashes the app. Setting
itemstonullwhen the code callsitems.mapthrows an error. This is useful: it shows a missing guard. Reload to recover. - Edits reset after a code change. Hot reloading or a remount discards edited state. Re-apply the edit.
Limits to remember
Live edits show how a component behaves with a given value. They do not prove the value can occur in production. After you reproduce a bug, trace how that state can arise, then write the fix and a test.
Exercise
You edit a prop on a child component in the Components tab and see the UI change. A moment later, after the parent updates, the original value is back. Why, and where should you edit instead?
Show answer
The parent passes the prop on every render, so its next render replaces your edit. Edit the state or hook in the parent component that supplies the value, because that change persists across re-renders.
Exercise
A list component crashes when you set its items prop to an empty array in the Components tab. What have you learned, and what is your next step?
Show answer
You have reproduced a bug for the empty state, probably code that assumes at least one item, such as reading items[0].name. Read the stack trace, add handling for the empty case, and then retest by editing items to an empty array again.