Skip to content

Commit accdc33

Browse files
committed
DevTools: support useEffectEvent and forward-fix experimental prefix support
1 parent 1185f88 commit accdc33

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-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-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
let React;
14+
let ReactDOMClient;
1415
let ReactTestRenderer;
1516
let ReactDebugTools;
1617
let act;
@@ -33,6 +34,7 @@ describe('ReactHooksInspectionIntegration', () => {
3334
beforeEach(() => {
3435
jest.resetModules();
3536
React = require('react');
37+
ReactDOMClient = require('react-dom/client');
3638
ReactTestRenderer = require('react-test-renderer');
3739
({act, assertConsoleErrorDev} = require('internal-test-utils'));
3840
ReactDebugTools = require('react-debug-tools');

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)