Profiler calling onRender for component updates outside of the Profiler tree
See original GitHub issueThe 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:
- Created 4 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It seems that the
Profiler fiber
is always marked asUpdate
inbeginWork
phase.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
.Given the above example:
A
B
C