Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools: support useEffectEvent and forward-fix experimental prefix support #32106

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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} />;
}
Loading