Skip to content

Commit

Permalink
[Flare] Adds useListener implementation to ReactDebugHooks (#16205)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Jul 25, 2019
1 parent ed72f40 commit 5b08f7b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* @flow
*/

import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';
import type {
ReactContext,
ReactProviderType,
ReactEventResponder,
} from 'shared/ReactTypes';
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import type {Hook} from 'react-reconciler/src/ReactFiberHooks';
import type {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactFiberHooks';
Expand All @@ -21,6 +25,8 @@ import {
ForwardRef,
} from 'shared/ReactWorkTags';

const emptyObject = {};

type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;

// Used to track hooks called during a render
Expand Down Expand Up @@ -215,8 +221,17 @@ function useMemo<T>(
return value;
}

function useListener() {
throw new Error('TODO: not yet implemented');
function useListener(
responder: ReactEventResponder<any, any>,
hookProps: ?Object,
): void {
const listenerProps = hookProps || emptyObject;
// Don't put the actual event responder object in, just its displayName
const value = {
responder: responder.displayName || 'EventResponder',
props: listenerProps,
};
hookLog.push({primitive: 'Listener', stackError: new Error(), value});
}

const Dispatcher: DispatcherType = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/

'use strict';

let React;
let ReactDebugTools;

describe('ReactHooksInspection', () => {
beforeEach(() => {
jest.resetModules();
const ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableFlareAPI = true;
React = require('react');
ReactDebugTools = require('react-debug-tools');
});

it('should inspect a simple useListener hook', () => {
const TestResponder = React.unstable_createResponder('TestResponder', {});

function Foo(props) {
React.unstable_useListener(TestResponder, {preventDefault: false});
return <div responders={<TestResponder />}>Hello world</div>;
}
let tree = ReactDebugTools.inspectHooks(Foo, {});
expect(tree).toEqual([
{
isStateEditable: false,
id: 0,
name: 'Listener',
value: {props: {preventDefault: false}, responder: 'TestResponder'},
subHooks: [],
},
]);
});
});

0 comments on commit 5b08f7b

Please sign in to comment.