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

[Flare] Adds useListener implementation to ReactDebugHooks #16205

Merged
merged 3 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 17 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,16 @@ 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;
trueadm marked this conversation as resolved.
Show resolved Hide resolved
const value = {
responder,
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
Expand Up @@ -332,4 +332,29 @@ describe('ReactHooksInspection', () => {
]);
});
});

it('should inspect a simple useListener hook', () => {
jest.resetModules();
const ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableFlareAPI = true;
React = require('react');
ReactDebugTools = require('react-debug-tools');

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: [],
},
]);
});
});