Home/Blog/Optimizing apps using React Developer Tools

Optimizing apps using React Developer Tools

This 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 Developer Tools, Components tab

React 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 conceptSimple ToDo List app concept

Open Chrome Developer tools, and you will see two new tabs: Components and Profiler.

React Developer Tools, Components tabReact Developer Tools, Components tab

The 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 DevToolsChanging component props inย real-time using React DevTools

React 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 tabReact DevTools Profiling tab

The 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 DevToolsRecord why each component rendered while profiling setting, React DevTools

Improving 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 modeReact DevTools Profiler opened inย Flame Graph mode

This 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 componentsReact DevTools flamegraph with aย few rendered components

Good! 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โ€ hintReact DevTools profiler showing the โ€œwhy did this renderโ€ hint

Adding 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 itemReact DevTools Profiler flamegraph with aย memoized item

Stepย 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 independentlyReact DevTools Profiler shows separated components rendering independently

The 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.

Ready to get started?

Join developers who use Uploadcare to build fileย handling quickly andย reliably.

Sign up for free