Skip to content

Commit

Permalink
Added Flow type to keep hooks dispatchers in-sync (#14599)
Browse files Browse the repository at this point in the history
* Added Flow type to keep hooks dispatchers in-sync
  • Loading branch information
bvaughn committed Jan 16, 2019
1 parent 4392e38 commit 7ab8a8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -209,7 +210,7 @@ function useMemo<T>(
return value;
}

const Dispatcher = {
const Dispatcher: DispatcherType = {
readContext,
useCallback,
useContext,
Expand Down
17 changes: 11 additions & 6 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -326,27 +327,31 @@ 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,
useReducer,
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.
Expand Down

0 comments on commit 7ab8a8e

Please sign in to comment.