Skip to content

Commit a3fccd2

Browse files
authored
Fix Profiler root change error (#18880)
1 parent 61f2a56 commit a3fccd2

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/ProfilerContext.js

+37-28
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {ProfilingDataFrontend} from './types';
2222
export type TabID = 'flame-chart' | 'ranked-chart' | 'interactions';
2323

2424
export type Context = {|
25-
// Which tab is selexted in the Profiler UI?
25+
// Which tab is selected in the Profiler UI?
2626
selectedTabID: TabID,
2727
selectTab(id: TabID): void,
2828

@@ -129,6 +129,38 @@ function ProfilerContextController({children}: Props) {
129129
setPrevProfilingData,
130130
] = useState<ProfilingDataFrontend | null>(null);
131131
const [rootID, setRootID] = useState<number | null>(null);
132+
const [selectedFiberID, selectFiberID] = useState<number | null>(null);
133+
const [selectedFiberName, selectFiberName] = useState<string | null>(null);
134+
135+
const selectFiber = useCallback(
136+
(id: number | null, name: string | null) => {
137+
selectFiberID(id);
138+
selectFiberName(name);
139+
140+
// Sync selection to the Components tab for convenience.
141+
if (id !== null) {
142+
const element = store.getElementByID(id);
143+
144+
// Keep in mind that profiling data may be from a previous session.
145+
// In that case, IDs may match up arbitrarily; to be safe, compare both ID and display name.
146+
if (element !== null && element.displayName === name) {
147+
dispatch({
148+
type: 'SELECT_ELEMENT_BY_ID',
149+
payload: id,
150+
});
151+
}
152+
}
153+
},
154+
[dispatch, selectFiberID, selectFiberName, store],
155+
);
156+
157+
const setRootIDAndClearFiber = useCallback(
158+
(id: number | null) => {
159+
selectFiber(null, null);
160+
setRootID(id);
161+
},
162+
[setRootID, selectFiber],
163+
);
132164

133165
if (prevProfilingData !== profilingData) {
134166
batchedUpdates(() => {
@@ -150,9 +182,9 @@ function ProfilerContextController({children}: Props) {
150182
selectedElementRootID !== null &&
151183
dataForRoots.has(selectedElementRootID)
152184
) {
153-
setRootID(selectedElementRootID);
185+
setRootIDAndClearFiber(selectedElementRootID);
154186
} else {
155-
setRootID(firstRootID);
187+
setRootIDAndClearFiber(firstRootID);
156188
}
157189
}
158190
}
@@ -180,34 +212,10 @@ function ProfilerContextController({children}: Props) {
180212
null,
181213
);
182214
const [selectedTabID, selectTab] = useState<TabID>('flame-chart');
183-
const [selectedFiberID, selectFiberID] = useState<number | null>(null);
184-
const [selectedFiberName, selectFiberName] = useState<string | null>(null);
185215
const [selectedInteractionID, selectInteraction] = useState<number | null>(
186216
null,
187217
);
188218

189-
const selectFiber = useCallback(
190-
(id: number | null, name: string | null) => {
191-
selectFiberID(id);
192-
selectFiberName(name);
193-
194-
// Sync selection to the Components tab for convenience.
195-
if (id !== null) {
196-
const element = store.getElementByID(id);
197-
198-
// Keep in mind that profiling data may be from a previous session.
199-
// In that case, IDs may match up arbitrarily; to be safe, compare both ID and display name.
200-
if (element !== null && element.displayName === name) {
201-
dispatch({
202-
type: 'SELECT_ELEMENT_BY_ID',
203-
payload: id,
204-
});
205-
}
206-
}
207-
},
208-
[dispatch, selectFiberID, selectFiberName, store],
209-
);
210-
211219
if (isProfiling) {
212220
batchedUpdates(() => {
213221
if (selectedCommitIndex !== null) {
@@ -237,7 +245,7 @@ function ProfilerContextController({children}: Props) {
237245
supportsProfiling,
238246

239247
rootID,
240-
setRootID,
248+
setRootID: setRootIDAndClearFiber,
241249

242250
isCommitFilterEnabled,
243251
setIsCommitFilterEnabled,
@@ -268,6 +276,7 @@ function ProfilerContextController({children}: Props) {
268276

269277
rootID,
270278
setRootID,
279+
setRootIDAndClearFiber,
271280

272281
isCommitFilterEnabled,
273282
setIsCommitFilterEnabled,

0 commit comments

Comments
 (0)