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

React: experimental_useEvent -> experimental_useEffectEvent #64880

Merged
merged 1 commit into from
Mar 24, 2023
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
2 changes: 1 addition & 1 deletion types/react/experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ declare module '.' {
export const SuspenseList: ExoticComponent<SuspenseListProps>;

// tslint:disable-next-line ban-types
export function experimental_useEvent<T extends Function>(event: T): T;
export function experimental_useEffectEvent<T extends Function>(event: T): T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function experimental_useEffectEvent<T extends Function>(event: T): T;
/**
* @see https://github.com/facebook/react/blob/9c54b29b44d24f8f8090da9c7ebf569747a444df/packages/react/src/ReactHooks.js#L237
*/
export function experimental_useEffectEvent<T extends Function>(event: T): T;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what value this would add. Especially since the flow types are wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, no problem. the value it would add is to point people to what this experimental thing actually does. not important.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link just pointed to the implementation calling the dispatcher. That's not actually telling what it was for. We almost always want to point to documentation not implementation.

}
35 changes: 35 additions & 0 deletions types/react/test/experimental.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import React = require('react');

// NOTE: forward declarations for tests
declare var console: Console;
interface Console {
log(...args: any[]): void;
}

function suspenseTest() {
function DisplayData() {
return null;
Expand Down Expand Up @@ -36,3 +42,32 @@ function suspenseTest() {
<React.Suspense fallback="Loading">A</React.Suspense>
<React.Suspense fallback="Loading">B</React.Suspense>
</React.SuspenseList>;

function useEvent() {
// Implicit any
// @ts-expect-error
const anyEvent = React.experimental_useEffectEvent(value => {
// $ExpectType any
return value;
});
// $ExpectType any
anyEvent({});
// $ExpectType (value: string) => number
const typedEvent = React.experimental_useEffectEvent((value: string) => {
return Number(value);
});
// $ExpectType number
typedEvent('1');
// Argument of type '{}' is not assignable to parameter of type 'string'.
// @ts-expect-error
typedEvent({});

function useContextuallyTypedEvent(fn: (event: Event) => string) {}
useContextuallyTypedEvent(
React.experimental_useEffectEvent(event => {
// $ExpectType Event
event;
return String(event);
}),
);
}
29 changes: 1 addition & 28 deletions types/react/test/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,34 +262,7 @@ const everyHookRef = React.createRef<{ id: number }>();
ref;
}}/>;

function useExperimentalHooks() {
// Implicit any
// @ts-expect-error
const anyEvent = React.experimental_useEvent(value => {
// $ExpectType any
return value;
});
// $ExpectType any
anyEvent({});
// $ExpectType (value: string) => number
const typedEvent = React.experimental_useEvent((value: string) => {
return Number(value);
});
// $ExpectType number
typedEvent('1');
// Argument of type '{}' is not assignable to parameter of type 'string'.
// @ts-expect-error
typedEvent({});

function useContextuallyTypedEvent(fn: (event: Event) => string) {}
useContextuallyTypedEvent(
React.experimental_useEvent(event => {
// $ExpectType Event
event;
return String(event);
}),
);

function useConcurrentHooks() {
const [toggle, setToggle] = React.useState(false);

const [done, startTransition] = React.useTransition();
Expand Down