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

Fix Flow types of useEffectEvent #26468

Merged
merged 4 commits into from
Mar 25, 2023
Merged
Changes from 3 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
12 changes: 7 additions & 5 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,26 @@ export function useSyncExternalStore<T>(

export function useCacheRefresh(): <T>(?() => T, ?T) => void {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
// $FlowFixMe[not-a-function] This is unstable, thus optional
return dispatcher.useCacheRefresh();
}

export function use<T>(usable: Usable<T>): T {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
// $FlowFixMe[not-a-function] This is unstable, thus optional
return dispatcher.use(usable);
}

export function useMemoCache(size: number): Array<any> {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
// $FlowFixMe[not-a-function] This is unstable, thus optional
return dispatcher.useMemoCache(size);
}

export function useEffectEvent<T>(callback: T): void {
export function useEffectEvent<Args, Return, F: (...Array<Args>) => Return>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kassens Do we actually need the Args and Return type parameter here? In TS we can just do useEffectEvent<T extends Function>. Does something similar exist in Flow?

Copy link
Member

Choose a reason for hiding this comment

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

This is a little bit shorter, we don't have to define the return type in the type constraint for F:

Suggested change
export function useEffectEvent<Args, Return, F: (...Array<Args>) => Return>(
export function useEffectEvent<Args, Return, F: (...Array<Args>) => Return>(
export function useEffectEvent<Args, F: (...Args) => mixed>(

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Applied in facebook/react@9cf21fb (#26468) to make it consistent with the internal types.

callback: F,
): F {
const dispatcher = resolveDispatcher();
// $FlowFixMe This is unstable, thus optional
// $FlowFixMe[not-a-function] This is unstable, thus optional
return dispatcher.useEffectEvent(callback);
}