Skip to content

Commit 33337c2

Browse files
committed
ref: change update logic
1 parent 371a247 commit 33337c2

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/react/src/profiler.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export type ProfilerProps = {
119119
disabled?: boolean;
120120
// If component updates should be displayed as spans. False by default.
121121
generateUpdateSpans?: boolean;
122+
// props from child component
123+
updateProps: { [key: string]: any };
122124
};
123125

124126
/**
@@ -174,13 +176,18 @@ class Profiler extends React.Component<ProfilerProps> {
174176

175177
public componentDidUpdate(prevProps: ProfilerProps): void {
176178
if (prevProps.generateUpdateSpans && this.span && prevProps !== this.props) {
177-
const now = timestampWithMs();
178-
this.span.startChild({
179-
description: `<${prevProps.name}>`,
180-
endTimestamp: now,
181-
op: `react.update`,
182-
startTimestamp: now,
183-
});
179+
const changedProps = Object.keys(prevProps).filter(k => prevProps.updateProps[k] !== this.props.updateProps[k]);
180+
if (changedProps.length > 0) {
181+
const now = timestampWithMs();
182+
const updateSpan = this.span.startChild({
183+
description: `<${prevProps.name}>`,
184+
endTimestamp: now,
185+
op: `react.update`,
186+
startTimestamp: now,
187+
});
188+
189+
updateSpan.setData('changedProps', changedProps);
190+
}
184191
}
185192
}
186193

@@ -209,13 +216,13 @@ class Profiler extends React.Component<ProfilerProps> {
209216
*/
210217
function withProfiler<P extends object>(
211218
WrappedComponent: React.ComponentType<P>,
212-
options?: Partial<ProfilerProps>,
219+
options?: Pick<Partial<ProfilerProps>, Exclude<keyof ProfilerProps, 'updateProps'>>,
213220
): React.FC<P> {
214221
const componentDisplayName =
215222
(options && options.name) || WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
216223

217224
const Wrapped: React.FC<P> = (props: P) => (
218-
<Profiler name={componentDisplayName} disabled={options && options.disabled}>
225+
<Profiler name={componentDisplayName} disabled={options && options.disabled} updateProps={props}>
219226
<WrappedComponent {...props} />
220227
</Profiler>
221228
);

0 commit comments

Comments
 (0)