Optimizing apps using React Developer Tools
Arthur KhayrullinThis article aims toย provide anย easy-to-follow tutorial onย the basics ofย optimizing React apps using React DevTools. Weย will put together aย simple unoptimized version ofย the ToDo app and gradually improve itย while uncovering React DevToolsโ functionality.
What isย React DevTools?
React Developer Tools isย aย set ofย tools that represent your appโs internals inย aย convenient fashion. Itย offers aย visual representation ofย the component hierarchy, real-time info onย state and props updates, and profiling tools toย inspect when and why components render.
How toย install React Developer Tools
React DevTools come inย the form ofย a browser extension for Chrome, Edge and Firefox, and asย anย npm package.
Inย this tutorial, weโll debug with Chrome. Download the extension from the Chrome Web Store toย get started.
For other browsers like Safari etc., install the react-devtools npm package
and inject the script into your websiteโs <head> tag.
After installing the extension, you should see Components and Profiler panels in your browserโs DevTools:
React Developer Tools, Components tabReact DevTools: Components tab
Iโd like this tutorial toย beย practice-driven, soย letโs put the tools toย the test byย analyzing our simple app. The app isย bootstrapped with create-react-app, soย feel free toย recreate the structure and copy code pieces provided inย this article. Itย looks asย follows:
Simple ToDo List app conceptOpen Chrome Developer tools, and you will see two new tabs: Components and Profiler.
React Developer Tools, Components tabThe Components tab isย Chromeโs Elements Inspector look-alike but for React. Highlight anย element inย the hierarchy tree (left panel) toย see its props, hooks, and other information (right panel).
You can change props inย real time like inย the screenshot below. Ifย the prop isย aย function, clicking redirects toย the Sources panel toย the respective line ofย code. This makes testing edge cases easier.
Hover over aย page element using the cursor icon left ofย the search field toย highlight its component inย the React DevTools tree. Toย reverseย it, select aย component inย the tree and click the eye icon inย the right panel toย highlight the element inย the Elements tab.
Changing component props inย real-time using React DevToolsReact DevTools: Profiler tab
The main goal ofย the Profiler tab isย toย record what happens onย the page and provide information about the performance ofย the components.
Start profiling byย clicking the โrecordโ icon onย the top-left onย the Profiler tab. Perform the actions that you want toย evaluate and click the icon again. This results inย the chart called โflamegraphโ like inย the screenshot below.
React DevTools Profiling tabThe Profiler records all the commits toย the DOM ofย the page that happened during the recording. Inย the screenshot above, there are 16ย commits and the 14th commit isย being viewed.
Aย commit itself consists ofย one orย more renders. Slow-loading component renders are yellow, fast-loading are blue, and gray bars are the components that didnโt render during this commit.
Inย this tutorial, weย will rely onย the โRecord why each component rendered while profilingโ setting. Click the gear icon onย the left ofย the commits slider toย enableย it.
Record why each component rendered while profiling setting, React DevToolsImproving performance using React DevTools: Anย example
Hereโs the code for the initial version ofย the app:
// App.jsx
import React, { useState } from 'react';
import './App.css';
function App() {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
const addTodo = () => {
if (inputValue.trim()) {
setTodos([...todos, { text: inputValue, id: Date.now() }]);
setInputValue('');
}
};
const deleteTodo = id => {
setTodos(todos.filter((todo) => todo.id !== id));
};
console.log('App rendered');
return (
<div className="app">
<h1>ToDo List</h1>
<input
type="text"
value={inputValue}
onChange={e => {
setInputValue(e.target.value);
console.log('Input changed');
}}
placeholder="Add a task"
/>
<button onClick={addTodo}>Add</button>
<ul>
{todos.map(todo => (
<li key={todo.id}>
{todo.text}
<button onClick={() => deleteTodo(todo.id)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default App;Stepย 1: Initial React DevTools profiler analysis
Letโs start byย gathering some info from Profiler. Iย started profiling the app, typed โtestโ 2ย times, added the item, and then deleted one ofย the items.
React DevTools Profiler opened inย Flame Graph modeThis resulted inย 11ย commits, and the App component got re-rendered inย each commit:
- 8ย commits total for each input field change
- 2ย commits for each click ofย the โAddโ button
- 1ย commit for to-do item removal
Weย canโt gather more information about the components rendering atย this pointย โ weโve got only one component that contains the whole app. And every time something changes orย triggers aย hook, the whole App component gets re-rendered. Letโs move the code for aย single to-do item into aย separate component.
Stepย 2: Separating to-do items
Separating the <li> item into its component:
// TodoItem.jsx
import React from 'react';
function TodoItem({ todo, onDelete }) {
console.log(`Rendering TodoItem ${todo.id}`);
return (
<li>
{todo.text}
<button onClick={() => onDelete(todo.id)}>Delete</button>
</li>
);
}
export default TodoItem;
// App.jsx
import React, { useState } from 'react';
import TodoItem from './TodoItem';
import './App.css';
function App() {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
const addTodo = () => {
if (inputValue.trim()) {
setTodos([...todos, { text: inputValue, id: Date.now() }]);
setInputValue('');
}
};
const deleteTodo = id => {
setTodos(todos.filter((todo) => todo.id !== id));
};
console.log('App rendered');
return (
<div className="app">
<h1>ToDo List</h1>
<input
type="text"
value={inputValue}
onChange={e => {
setInputValue(e.target.value);
console.log('Input changed');
}}
placeholder="Add a task"
/>
<button onClick={addTodo}>Add</button>
<ul>
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} onDelete={deleteTodo} />
))}
</ul>
</div>
);
}
export default App;Performing the same testing steps leads toย the following picture:
React DevTools flamegraph with aย few rendered componentsGood! Although the App component still gets re-rendered inย each commit, now weย have more control over the items moved into the separate component.
Iย recorded adding two independent TodoItem component instances, but when adding the second one,
the first item (obscured byย aย tooltip onย the screenshot) got re-rendered too.
Hovering the item givesย us more details: re-rendering happened due toย the change ofย the onDelete prop.
Stepย 3: Add memoization and stabilize functions
Each time the App component re-renders, new addTodo and deleteTodo functions are created.
Even though the functionย JS code stays the same, the TodoItem component registers this asย aย prop change and re-renders.
Weย can use the useCallback hook toย โcacheโ functions and pass them asย arguments into the TodoItem component.
Letโs also implement memoization within the TodoItem component itself.
React.memo() allows aย component toย skip re-rendering when its props are unchanged,
even ifย its parent component isย re-rendered (which is App inย our case).
// TodoItem.jsx
import React from 'react';
const TodoItem = React.memo(({ todo, onDelete }) => {
console.log(`Rendering TodoItem ${todo.id}`);
return (
<li>
{todo.text}
<button onClick={() => onDelete(todo.id)}>Delete</button>
</li>
);
});
export default TodoItem;
// App.jsx
import React, { useState, useCallback } from 'react';
import TodoItem from './TodoItem';
import './App.css';
function App() {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
const addTodo = useCallback(() => {
if (inputValue.trim()) {
setTodos((prev) => [...prev, { text: inputValue, id: Date.now() }]);
setInputValue('');
}
}, [inputValue]);
const deleteTodo = useCallback(id => {
setTodos((prev) => prev.filter((todo) => todo.id !== id));
}, []);
console.log('App rendered');
return (
<div className="app">
<h1>ToDo List</h1>
<input
type="text"
value={inputValue}
onChange={e => {
setInputValue(e.target.value);
console.log('Input changed');
}}
placeholder="Add a task"
/>
<button onClick={addTodo}>Add</button>
<ul>
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} onDelete={deleteTodo} />
))}
</ul>
</div>
);
}
export default App;After running the test with useCallback only, weย see that the TodoItem component still re-renders,
but now for aย different reason (The parent component rendered):
React DevTools profiler showing the โwhy did this renderโ hintAdding React.memo() into the TodoItem component improves the situation even further.
Aย memoized item turned grey, this means itย did not re-render inย the given commit:
React DevTools Profiler flamegraph with aย memoized itemStepย 4: Isolating input state
The App component changes its state every time the onChange event isย triggered onย the input field.
Toย prevent this from happening, letโs move the input field into aย separate component, TodoInput.
// TodoInput.jsx
import React, { useState } from 'react';
function TodoInput({ onAdd }) {
const [inputValue, setInputValue] = useState('');
console.log('TodoInput rendered');
const handleAdd = () => {
if (inputValue.trim()) {
onAdd(inputValue);
setInputValue('');
}
};
return (
<>
<input
type="text"
value={inputValue}
onChange={e => {
setInputValue(e.target.value);
console.log('Input changed');
}}
placeholder="Add a task"
/>
<button onClick={handleAdd}>Add</button>
</>
);
}
export default TodoInput;
// App.jsx
import React, { useState, useCallback } from 'react';
import TodoInput from './TodoInput';
import TodoItem from './TodoItem';
import './App.css';
function App() {
const [todos, setTodos] = useState([]);
const addTodo = useCallback(text => {
setTodos((prev) => [...prev, { text, id: Date.now() }]);
}, []);
const deleteTodo = useCallback(id => {
setTodos((prev) => prev.filter((todo) => todo.id !== id));
}, []);
console.log('App rendered');
return (
<div className="app">
<h1>ToDo List</h1>
<TodoInput onAdd={addTodo} />
<ul>
{todos.map(todo => (
<TodoItem key={todo.id} todo={todo} onDelete={deleteTodo} />
))}
</ul>
</div>
);
}
export default App;Running the test again:
React DevTools Profiler shows separated components rendering independentlyThe TodoInput component now does all the heavy lifting when the input field changes,
and the App component renders only when the tasks list isย updated.
Aย note onย optimizing apps asย aย whole
The example app given inย this article isย rather onย the simpler side ofย the web development spectrum. The performance ofย content-heavy apps usually depends onย several factors. While itย isย possible for aย developer toย optimize and maintain efficient front-end and back-end performance, getting the network part ofย the equation under control for all visitors might beย tricky.
Uploadcare offers aย convenient way toย upload, store, transform, optimize, and deliver images, videos, and documents. With the widely-distributed Content Delivery Network, the platform accelerates the loading ofย the media assets toย your visitors. Talking inย React terms, ifย thereโs aย component that isย suspended until some media isย loaded, this component would render faster.
Wrapping up
Getting good at React Developer Tools isnโt just about speeding up apps โ itโs changing the way we build them, helping us plan ahead instead of just fixing things. With our ToDo app, we saw how simple changes like keeping state separate or reusing functions can make a big difference. For larger apps with lots of images or videos, using something like Uploadcare Image CDN alongside these tricks can make things not only faster, but better for users. Itโs about working smarter, not just speeding things up.