Sub topic 7
Using the Console and source links with a selected component
This chapter assumes you can select a component in the Components tab and read its props, state, and hooks. It also assumes you know the basics of the browser Console. Here you will use the selected component from the Console, and you will jump from a component to its source code.
Why leave the Components tab
The Components tab shows values, but it is a read-only list. The Console lets you run code against a component. You can read a nested prop, check its type, or call a method. This is faster than expanding many levels of an object in the side panel.
The `$r` variable
When you select a component in the Components tab, React Developer Tools stores it in a Console variable named $r. Switch to the Console, and $r refers to the component you last selected.
// In the Components tab, select a component, then switch to the Console:
$r.props; // props of the selected component
$r.type; // the component function or class itself$r.props returns the props object. $r.type returns the component function or class. For a class component, $r is the instance, so $r.state and $r.setState are also available.
The $r variable is useful in a few cases:
- Reading a large array in props and filtering it, for example
$r.props.items.filter(Boolean).length. - Checking which function a prop holds, for example
$r.props.onSelect.toString(). - Logging a value and using the Console's object viewer to expand it.
Changing $r.props from the Console does not update the screen. Props belong to the parent component, so use the editing feature from the previous chapter to change values.
Related tools in the Components panel
The panel for a selected component has buttons in its header. Three of them connect to the Console and the source code.
| Button | What it does |
|---|---|
| Log this component data to the console | Prints the component's props, state, and hooks to the Console |
| Show the matching DOM element in the Elements panel | Selects the DOM node this component rendered |
| View source for this element | Opens the component's source in the Sources panel |
The log button is useful when you want a snapshot. The Console prints the object at that moment, so you can compare it with a later snapshot after an interaction.
Viewing source
The view source button opens the file where the component is defined, in the browser's Sources panel. You can then set a breakpoint in the component body or in an event handler. A breakpoint pauses the code so you can step through it line by line.
Source links show the code your bundler produced. If the bundle has source maps, the Sources panel shows your original files. Development servers such as Vite and Create React App provide source maps by default.
A short workflow
- Select the component that shows wrong data.
- In the Console, type
$r.propsand check the values. - Use the view source button to open the component's file.
- Set a breakpoint, repeat the action in the app, and step through the code.
Common errors
$ris undefined. No component has been selected yet. Select one in the Components tab first.- The source button is missing or disabled. The component may come from a library without source maps, or it may be a built-in element.
- The Sources panel shows minified code. The build has no source maps. Use a development build.
- Console edits do not change the UI. Changing an object from the Console does not make React render again.
Exercise
You selected a List component in the Components tab, switched to the Console, and typed $r.props.items.length. It returned undefined and raised an error. Name two likely causes.
Show answer
Either no component is selected, so $r is undefined, or the selected component does not have an items prop, so $r.props.items is undefined. Another component may have been selected after List, which would change what $r points to. Select List again and check $r.props.