Skip to content

Commit

Permalink
DevTools: support useEffectEvent and forward-fix experimental prefix …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
hoxyq committed Jan 17, 2025
1 parent 1185f88 commit accdc33
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
}

Dispatcher.useId();

if (typeof Dispatcher.useResourceEffect === 'function') {
Dispatcher.useResourceEffect(() => ({}), []);
}
if (typeof Dispatcher.useEffectEvent === 'function') {
Dispatcher.useEffectEvent((args: empty) => {});
}
} finally {
readHookLog = hookLog;
hookLog = [];
Expand Down Expand Up @@ -749,6 +756,20 @@ function useResourceEffect(
});
}

function useEffectEvent<Args, F: (...Array<Args>) => mixed>(callback: F): F {
nextHook();
hookLog.push({
displayName: null,
primitive: 'EffectEvent',
stackError: new Error(),
value: callback,
debugInfo: null,
dispatcherHookName: 'EffectEvent',
});

return callback;
}

const Dispatcher: DispatcherType = {
use,
readContext,
Expand All @@ -773,6 +794,7 @@ const Dispatcher: DispatcherType = {
useFormState,
useActionState,
useHostTransitionStatus,
useEffectEvent,
useResourceEffect,
};

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

if (functionName.slice(startIndex).startsWith('unstable_')) {
if (functionName.slice(startIndex).startsWith('experimental_')) {
startIndex += 'experimental_'.length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

let React;
let ReactDOMClient;

Check failure on line 14 in packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

View workflow job for this annotation

GitHub Actions / Run eslint

'ReactDOMClient' is assigned a value but never used
let ReactTestRenderer;
let ReactDebugTools;
let act;
Expand All @@ -33,6 +34,7 @@ describe('ReactHooksInspectionIntegration', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOMClient = require('react-dom/client');
ReactTestRenderer = require('react-test-renderer');
({act, assertConsoleErrorDev} = require('internal-test-utils'));
ReactDebugTools = require('react-debug-tools');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import NestedProps from './NestedProps';
import SimpleValues from './SimpleValues';
import SymbolKeys from './SymbolKeys';
import UseMemoCache from './UseMemoCache';
import UseEffectEvent from './UseEffectEvent';

// TODO Add Immutable JS example

Expand All @@ -36,6 +37,7 @@ export default function InspectableElements(): React.Node {
<CircularReferences />
<SymbolKeys />
<UseMemoCache />
<UseEffectEvent />
</Fragment>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from 'react';

const {experimental_useEffectEvent, useState, useEffect} = React;

export default function UseEffectEvent(): React.Node {
return (
<>
<SingleHookCase />
<HookTreeCase />
</>
);
}

function SingleHookCase() {
const onClick = experimental_useEffectEvent(() => {});

return <div onClick={onClick} />;
}

function useCustomHook() {
const [state, setState] = useState();
const onClick = experimental_useEffectEvent(() => {});
useEffect(() => {});

return [state, setState, onClick];
}

function HookTreeCase() {
const onClick = useCustomHook();

return <div onClick={onClick} />;
}

0 comments on commit accdc33

Please sign in to comment.