Skip to content

Profiler calling onRender for component updates outside of the Profiler tree #16994

@kentcdodds

Description

@kentcdodds

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?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions