{"componentChunkName":"component---src-templates-docs-js","path":"/docs/profiler.html","result":{"data":{"markdownRemark":{"html":"
\n
\n

These docs are old and won’t be updated. Go to react.dev for the new React docs.

\n

These new documentation pages teach modern React:

\n\n
\n
\n

The Profiler measures how often a React application renders and what the “cost” of rendering is.\nIts purpose is to help identify parts of an application that are slow and may benefit from optimizations such as memoization.

\n
\n

Note:

\n

Profiling adds some additional overhead, so it is disabled in the production build.

\n

To opt into production profiling, React provides a special production build with profiling enabled.\nRead more about how to use this build at fb.me/react-profiling

\n
\n

Usage

\n

A Profiler can be added anywhere in a React tree to measure the cost of rendering that part of the tree.\nIt requires two props: an id (string) and an onRender callback (function) which React calls any time a component within the tree “commits” an update.

\n

For example, to profile a Navigation component and its descendants:

\n
render(\n  <App>\n    <Profiler id=\"Navigation\" onRender={callback}>      <Navigation {...props} />\n    </Profiler>\n    <Main {...props} />\n  </App>\n);
\n

Multiple Profiler components can be used to measure different parts of an application:

\n
render(\n  <App>\n    <Profiler id=\"Navigation\" onRender={callback}>      <Navigation {...props} />\n    </Profiler>\n    <Profiler id=\"Main\" onRender={callback}>      <Main {...props} />\n    </Profiler>\n  </App>\n);
\n

Profiler components can also be nested to measure different components within the same subtree:

\n
render(\n  <App>\n    <Profiler id=\"Panel\" onRender={callback}>      <Panel {...props}>\n        <Profiler id=\"Content\" onRender={callback}>          <Content {...props} />\n        </Profiler>\n        <Profiler id=\"PreviewPane\" onRender={callback}>          <PreviewPane {...props} />\n        </Profiler>\n      </Panel>\n    </Profiler>\n  </App>\n);
\n
\n

Note

\n

Although Profiler is a light-weight component, it should be used only when necessary; each use adds some CPU and memory overhead to an application.

\n
\n

onRender Callback

\n

The Profiler requires an onRender function as a prop.\nReact calls this function any time a component within the profiled tree “commits” an update.\nIt receives parameters describing what was rendered and how long it took.

\n
function onRenderCallback(\n  id, // the \"id\" prop of the Profiler tree that has just committed\n  phase, // either \"mount\" (if the tree just mounted) or \"update\" (if it re-rendered)\n  actualDuration, // time spent rendering the committed update\n  baseDuration, // estimated time to render the entire subtree without memoization\n  startTime, // when React began rendering this update\n  commitTime, // when React committed this update\n  interactions // the Set of interactions belonging to this update\n) {\n  // Aggregate or log render timings...\n}
\n

Let’s take a closer look at each of the props:

\n\n
\n

Note

\n

Interactions can be used to identify the cause of an update, although the API for tracing them is still experimental.

\n

Learn more about it at fb.me/react-interaction-tracing

\n
","frontmatter":{"title":"Profiler API","next":null,"prev":null},"fields":{"path":"content/docs/reference-profiler.md","slug":"docs/profiler.html"}}},"pageContext":{"slug":"docs/profiler.html"}},"staticQueryHashes":[]}