Skip to content

Commit b000019

Browse files
authored
DevTools: support useEffectEvent and forward-fix experimental prefix support (#32106)
- Adds support for `experimental_useEffectEvent`, now DevTools will be able to display this hook for inspected element - Added a use case to DevTools shell, couldn't add case, because we are using ReactTestRenderer, which has the corresponding flag disabled. - Forward-fix logic for handling `experimental` prefix that was added in #32088. ![Screenshot 2025-01-16 at 21 24 12](https://github.com/user-attachments/assets/6fb8ff2a-be47-47b5-bbfc-73d3a586657c)
1 parent 028c8e6 commit b000019

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
127127
}
128128

129129
Dispatcher.useId();
130+
131+
if (typeof Dispatcher.useResourceEffect === 'function') {
132+
Dispatcher.useResourceEffect(() => ({}), []);
133+
}
134+
if (typeof Dispatcher.useEffectEvent === 'function') {
135+
Dispatcher.useEffectEvent((args: empty) => {});
136+
}
130137
} finally {
131138
readHookLog = hookLog;
132139
hookLog = [];
@@ -749,6 +756,20 @@ function useResourceEffect(
749756
});
750757
}
751758

759+
function useEffectEvent<Args, F: (...Array<Args>) => mixed>(callback: F): F {
760+
nextHook();
761+
hookLog.push({
762+
displayName: null,
763+
primitive: 'EffectEvent',
764+
stackError: new Error(),
765+
value: callback,
766+
debugInfo: null,
767+
dispatcherHookName: 'EffectEvent',
768+
});
769+
770+
return callback;
771+
}
772+
752773
const Dispatcher: DispatcherType = {
753774
use,
754775
readContext,
@@ -773,6 +794,7 @@ const Dispatcher: DispatcherType = {
773794
useFormState,
774795
useActionState,
775796
useHostTransitionStatus,
797+
useEffectEvent,
776798
useResourceEffect,
777799
};
778800

@@ -962,7 +984,7 @@ function parseHookName(functionName: void | string): string {
962984
startIndex += 'unstable_'.length;
963985
}
964986

965-
if (functionName.slice(startIndex).startsWith('unstable_')) {
987+
if (functionName.slice(startIndex).startsWith('experimental_')) {
966988
startIndex += 'experimental_'.length;
967989
}
968990

packages/react-devtools-shell/src/app/InspectableElements/InspectableElements.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import NestedProps from './NestedProps';
1919
import SimpleValues from './SimpleValues';
2020
import SymbolKeys from './SymbolKeys';
2121
import UseMemoCache from './UseMemoCache';
22+
import UseEffectEvent from './UseEffectEvent';
2223

2324
// TODO Add Immutable JS example
2425

@@ -36,6 +37,7 @@ export default function InspectableElements(): React.Node {
3637
<CircularReferences />
3738
<SymbolKeys />
3839
<UseMemoCache />
40+
<UseEffectEvent />
3941
</Fragment>
4042
);
4143
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
3+
const {experimental_useEffectEvent, useState, useEffect} = React;
4+
5+
export default function UseEffectEvent(): React.Node {
6+
return (
7+
<>
8+
<SingleHookCase />
9+
<HookTreeCase />
10+
</>
11+
);
12+
}
13+
14+
function SingleHookCase() {
15+
const onClick = experimental_useEffectEvent(() => {});
16+
17+
return <div onClick={onClick} />;
18+
}
19+
20+
function useCustomHook() {
21+
const [state, setState] = useState();
22+
const onClick = experimental_useEffectEvent(() => {});
23+
useEffect(() => {});
24+
25+
return [state, setState, onClick];
26+
}
27+
28+
function HookTreeCase() {
29+
const onClick = useCustomHook();
30+
31+
return <div onClick={onClick} />;
32+
}

0 commit comments

Comments
 (0)