Added Flow type to keep hooks dispatchers in-sync (#14599)

* Added Flow type to keep hooks dispatchers in-sync
This commit is contained in:
Brian Vaughn 2019-01-16 12:49:31 -08:00 committed by GitHub
parent 4392e3821d
commit 7ab8a8e979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -10,6 +10,7 @@
import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import type {Hook} from 'react-reconciler/src/ReactFiberHooks';
import typeof {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactFiberDispatcher';
import ErrorStackParser from 'error-stack-parser';
import ReactSharedInternals from 'shared/ReactSharedInternals';
@ -209,7 +210,7 @@ function useMemo<T>(
return value;
}
const Dispatcher = {
const Dispatcher: DispatcherType = {
readContext,
useCallback,
useContext,

View File

@ -7,6 +7,7 @@
* @flow
*/
import typeof {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactFiberDispatcher';
import type {ThreadID} from './ReactThreadIDAllocator';
import type {ReactContext} from 'shared/ReactTypes';
import areHookInputsEqual from 'shared/areHookInputsEqual';
@ -326,18 +327,23 @@ function dispatchAction<A>(
}
}
function noop(): void {}
function identity(fn: Function): Function {
return fn;
export function useCallback<T>(
callback: T,
inputs: Array<mixed> | void | null,
): T {
// Callbacks are passed as they are in the server environment.
return callback;
}
function noop(): void {}
export let currentThreadID: ThreadID = 0;
export function setCurrentThreadID(threadID: ThreadID) {
currentThreadID = threadID;
}
export const Dispatcher = {
export const Dispatcher: DispatcherType = {
readContext,
useContext,
useMemo,
@ -345,8 +351,7 @@ export const Dispatcher = {
useRef,
useState,
useLayoutEffect,
// Callbacks are passed as they are in the server environment.
useCallback: identity,
useCallback,
// useImperativeHandle is not run in the server environment
useImperativeHandle: noop,
// Effects are not run in the server environment.