
getDerivedStateFromProps (now it will be called at each render). I have not used it enough yet, so for me this update was not very important.unstable_Profiler . Seeing that my life is unstable enough now ( unstable_ ), I decided to read the RFC and try it.Profiler component.unstable_ ). Later they will finish the ability to profile and production code.Profiler is a component that can be extracted from the React package by default. Since he has a cautious underscore name that many linters swear at, you can get around this as follows: import React, { unstable_Profiler as Profiler } from 'react'; ... const Profiler = React.unstable_Profiler; Profiler , let's take a look at the components! You can wrap any part of your JSX tree in Profiler to see what happens to it. Profiler accepts an onRender function, in which information about rendering time is recorded. Here is a simple counter example : import React, { unstable_Profiler as Profiler } from 'react'; class ComponentWithProfiling extends React.Component { state = { count: 0 }; logProfile = (id, phase, actualTime, baseTime, startTime, commitTime) => { console.log(`${id}'s ${phase} phase:`); console.log(`Actual time: ${actualTime}`); console.log(`Base time: ${baseTime}`); console.log(`Start time: ${startTime}`); console.log(`Commit time: ${commitTime}`); }; go = direction => () => this.setState(({ count }) => ({ count: direction === "up" ? count + 1 : count - 1 })); render() { return ( <Profiler id="app" onRender={this.logProfile}> <button onClick={this.go("up")}>️</button> <div>The count is {this.state.count}</div> <button onClick={this.go("down")}></button> </Profiler> ); } } id each fragment that you profile. As you can see below, onRender accepts a bunch of interesting metrics:
mount or update ), which can be used to identify parts of the code that are updated unexpectedly (just like the excellent why-did-you-update package I've used many times and highly recommend).actualTime and baseTime . They are related to the actual time React spends on rendering calculations; those. finding out what has changed. Note that the actual time (actual time) of the initial connection (mount) is longer, the update time (update). This is because at the initial connection technically everything is “new”. While updating, calculations should be simpler, because, I hope, the components in the tree are updated only if they really changed (that is, when the values ​​of prop / states changed).shouldComponentUpdate used incorrectly or not used at all, places where reference types are often changed or new ones are transmitted, or just places where you didn’t expect updates to take so long.onRender are startTime and commitTime . This is, in fact, timestamps since the initial launch. startTime is the time from which the selected component began to perform calculations for drawing, whereas commitTime is the time when React actually recorded these changes when drawing.Profiler in the future. Thanks @bvaughn for adding this elegant feature!Source: https://habr.com/ru/post/428998/
All Articles