Commit 53daaf5
Improve the detection of changed hooks (facebook#35123)
## Summary
cc @hoxyq
Fixes facebook#28584. Follow up to PR:
facebook#34547
This PR updates getChangedHooksIndices to account for the fact that
`useSyncExternalStore`, `useTransition`, `useActionState`,
`useFormState` internally mounts more than one hook while DevTools
should treat it as a single user-facing hook.
Approach idea came from
[this](facebook#34547 (comment))
comment 😄
Before:
https://github.com/user-attachments/assets/6bd5ce80-8b52-4bb8-8bb1-5e91b1e65043
After:
https://github.com/user-attachments/assets/47f56898-ab34-46b6-be7a-a54024dcefee
## How did you test this change?
I used this component to reproduce this issue locally (I followed
instructions in `packages/react-devtools/CONTRIBUTING.md`).
<details><summary>Details</summary>
```ts
import * as React from 'react';
function useDeepNestedHook() {
React.useState(0); // 1
return React.useState(1); // 2
}
function useNestedHook() {
const deepState = useDeepNestedHook();
React.useState(2); // 3
React.useState(3); // 4
return deepState;
}
// Create a simple store for useSyncExternalStore
function createStore(initialValue) {
let value = initialValue;
const listeners = new Set();
return {
getSnapshot: () => value,
subscribe: listener => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
},
update: newValue => {
value = newValue;
listeners.forEach(listener => listener());
},
};
}
const syncExternalStore = createStore(0);
export default function InspectableElements(): React.Node {
const [nestedState, setNestedState] = useNestedHook();
// 5
const syncExternalValue = React.useSyncExternalStore(
syncExternalStore.subscribe,
syncExternalStore.getSnapshot,
);
// 6
const [isPending, startTransition] = React.useTransition();
// 7
const [formState, formAction, formPending] = React.useActionState(
async (prevState, formData) => {
return {count: (prevState?.count || 0) + 1};
},
{count: 0},
);
const handleTransition = () => {
startTransition(() => {
setState(Math.random());
});
};
// 8
const [state, setState] = React.useState('test');
return (
<>
<div
style={{
padding: '20px',
display: 'flex',
flexDirection: 'column',
gap: '10px',
}}>
<div
onClick={() => setNestedState(Math.random())}
style={{backgroundColor: 'red', padding: '10px', cursor: 'pointer'}}>
State: {nestedState}
</div>
<button onClick={handleTransition} style={{padding: '10px'}}>
Trigger Transition {isPending ? '(pending...)' : ''}
</button>
<div style={{display: 'flex', gap: '10px', alignItems: 'center'}}>
<button
onClick={() => syncExternalStore.update(syncExternalValue + 1)}
style={{padding: '10px'}}>
Trigger useSyncExternalStore
</button>
<span>Value: {syncExternalValue}</span>
</div>
<form
action={formAction}
style={{display: 'flex', gap: '10px', alignItems: 'center'}}>
<button
type="submit"
style={{padding: '10px'}}
disabled={formPending}>
Trigger useFormState {formPending ? '(pending...)' : ''}
</button>
<span>Count: {formState.count}</span>
</form>
<div
onClick={() => setState(Math.random())}
style={{backgroundColor: 'red', padding: '10px', cursor: 'pointer'}}>
State: {state}
</div>
</div>
</>
);
}
```
</details>
---------
Co-authored-by: Ruslan Lesiutin <28902667+hoxyq@users.noreply.github.com>1 parent 4a3d993 commit 53daaf5
4 files changed
+290
-68
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
470 | | - | |
| 470 | + | |
471 | 471 | | |
472 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
473 | 475 | | |
474 | 476 | | |
475 | 477 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
| 113 | + | |
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| |||
Lines changed: 246 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
381 | 387 | | |
382 | 388 | | |
383 | 389 | | |
| |||
388 | 394 | | |
389 | 395 | | |
390 | 396 | | |
391 | | - | |
| 397 | + | |
392 | 398 | | |
393 | 399 | | |
394 | | - | |
| 400 | + | |
395 | 401 | | |
396 | 402 | | |
397 | 403 | | |
| |||
521 | 527 | | |
522 | 528 | | |
523 | 529 | | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
524 | 762 | | |
525 | 763 | | |
526 | 764 | | |
| |||
553 | 791 | | |
554 | 792 | | |
555 | 793 | | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
556 | 799 | | |
557 | 800 | | |
558 | 801 | | |
| |||
567 | 810 | | |
568 | 811 | | |
569 | 812 | | |
570 | | - | |
| 813 | + | |
571 | 814 | | |
572 | 815 | | |
573 | 816 | | |
| |||
0 commit comments