@@ -79,6 +79,7 @@ import {
7979 MEMO_SYMBOL_STRING ,
8080} from './ReactSymbols' ;
8181import { format } from './utils' ;
82+ import { enableProfilerChangedHookIndices } from 'react-devtools-feature-flags' ;
8283
8384import type { Fiber } from 'react-reconciler/src/ReactInternalTypes' ;
8485import type {
@@ -978,12 +979,9 @@ export function attach(
978979 state : null ,
979980 } ;
980981 } else {
981- return {
982+ const data : ChangeDescription = {
982983 context : getContextChangedKeys ( nextFiber ) ,
983- didHooksChange : didHooksChange (
984- prevFiber . memoizedState ,
985- nextFiber . memoizedState ,
986- ) ,
984+ didHooksChange : false ,
987985 isFirstMount : false ,
988986 props : getChangedKeys (
989987 prevFiber . memoizedProps ,
@@ -994,6 +992,23 @@ export function attach(
994992 nextFiber . memoizedState ,
995993 ) ,
996994 } ;
995+
996+ // Only traverse the hooks list once, depending on what info we're returning.
997+ if ( enableProfilerChangedHookIndices ) {
998+ const indices = getChangedHooksIndices (
999+ prevFiber . memoizedState ,
1000+ nextFiber . memoizedState ,
1001+ ) ;
1002+ data . hooks = indices ;
1003+ data . didHooksChange = indices !== null && indices . length > 0 ;
1004+ } else {
1005+ data . didHooksChange = didHooksChange (
1006+ prevFiber . memoizedState ,
1007+ nextFiber . memoizedState ,
1008+ ) ;
1009+ }
1010+
1011+ return data;
9971012 }
9981013 default :
9991014 return null ;
@@ -1154,6 +1169,36 @@ export function attach(
11541169 return false ;
11551170 }
11561171
1172+ function getChangedHooksIndices ( prev : any , next : any ) : null | Array < number > {
1173+ if ( enableProfilerChangedHookIndices ) {
1174+ if ( prev == null || next == null ) {
1175+ return null ;
1176+ }
1177+
1178+ const indices = [ ] ;
1179+ let index = 0 ;
1180+ if (
1181+ next . hasOwnProperty ( 'baseState' ) &&
1182+ next . hasOwnProperty ( 'memoizedState' ) &&
1183+ next . hasOwnProperty ( 'next' ) &&
1184+ next . hasOwnProperty ( 'queue' )
1185+ ) {
1186+ while ( next !== null ) {
1187+ if ( didHookChange ( prev , next ) ) {
1188+ indices . push ( index ) ;
1189+ }
1190+ next = next . next ;
1191+ prev = prev . next ;
1192+ index ++ ;
1193+ }
1194+ }
1195+
1196+ return indices ;
1197+ }
1198+
1199+ return null ;
1200+ }
1201+
11571202 function getChangedKeys ( prev : any , next : any ) : null | Array < string > {
11581203 if ( prev == null || next == null ) {
11591204 return null ;
0 commit comments