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

View File

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