question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Profiler calling onRender for component updates outside of the Profiler tree

See original GitHub issue

From the Profiler docs:

The Profiler requires an onRender function as a prop. React calls this function any time a component within the profiled tree “commits” an update.

However, given:

import React from 'react'
import ReactDOM from 'react-dom'

function Counter() {
  const [count, setCount] = React.useState(0)
  const increment = () => setCount(c => c + 1)
  return <button onClick={increment}>{count}</button>
}

function App() {
  return (
    <div>
      <React.Profiler
        id="counter"
        onRender={() => console.log('called')}
      >
        <div>
          Profiled counter
          <Counter />
        </div>
      </React.Profiler>
      <div>
        Unprofiled counter
        <Counter />
      </div>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

I’m getting a log when I click on the unprofiled counter.

Reproduction: https://codesandbox.io/s/react-codesandbox-tnff6

Am I misunderstanding this API, or is this a bug?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
stonexercommented, Oct 6, 2019

It seems that the Profiler fiber is always marked as Update in beginWork phase.

1reaction
bvaughncommented, Oct 3, 2019

I don’t think it has anything to do with the element type, but where they are declared relative to where the <Profiler> tags are defined.

It looks like for a subtree that belongs to a single owner- rendering further down in the subtree will cause the Profiler higher up to call onRender.

const Bar = () => (
  <>
    <Profiler id="A">
      <A />
    </Profiler>
    <div>
      <Profiler id="B">
        <B />
      </Profiler>
    </div>
  </>
);

const Foo = () => (
  <Profiler id="root">
    <Bar />
    <C />
  </Profiler>
);

Given the above example:

A B root
update A
update B
update C
Read more comments on GitHub >

github_iconTop Results From Across the Web

Profiler API - React
The Profiler requires an onRender function as a prop. React calls this function any time a component within the profiled tree “commits” an...
Read more >
Notes about the in-development React <Profiler> component
Profiler can be declared anywhere within a React tree to measure the cost of rendering that ... The onRender callback is called each...
Read more >
How to check if your component rerendered - and why!
To enable it, go to "Profiler" >> click the "Cog wheel" on the right side of ... tab >> Check the "Highlight updates...
Read more >
Making renders faster with the React 16.5 profiler
This causes render to get called for posts all the way at the top, even though they haven't changed! This seemed like a...
Read more >
The definitive guide to profiling React applications
The React Devtools extension; The Profiler Component ... During this phase, React calls render and then compares the result to the previous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found