Skip to content

Commit

Permalink
Moved ref to its own section, without a head, displayed above props
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn authored and eps1lon committed Jul 3, 2022
1 parent 7ad89dc commit 3fc0c2e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2927,14 +2927,9 @@ describe('InspectedElement', () => {
await utils.actAsync(() => legacyRender(<Example />, container));

const inspectedElement = await inspectElementAtIndex(1);
expect(inspectedElement.ref).toMatchInlineSnapshot(`
Object {
"current": Dehydrated {
"preview_short": <div />,
"preview_long": <div />,
},
}
`);
expect(inspectedElement.ref).toMatchInlineSnapshot(
`"{current: <div />}"`,
);
});

it('supports useRef on class components', async () => {
Expand All @@ -2951,7 +2946,7 @@ describe('InspectedElement', () => {
await utils.actAsync(() => legacyRender(<Example />, container));

const inspectedElement = await inspectElementAtIndex(1);
expect(inspectedElement.ref).toEqual({current: expect.any(Object)});
expect(inspectedElement.ref).toEqual('{current: {…}}');
});

it('supports ref callbacks', async () => {
Expand All @@ -2966,15 +2961,7 @@ describe('InspectedElement', () => {
await utils.actAsync(() => legacyRender(<Example />, container));

const inspectedElement = await inspectElementAtIndex(1);
expect(inspectedElement.ref).toMatchInlineSnapshot(`
Object {
"inspectable": false,
"name": "ref",
"preview_long": "ƒ ref() {}",
"preview_short": "ƒ ref() {}",
"type": "function",
}
`);
expect(inspectedElement.ref).toMatchInlineSnapshot(`"ƒ ref() {}"`);
});
});
});
11 changes: 8 additions & 3 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
utfEncodeString,
} from 'react-devtools-shared/src/utils';
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
import {formatDataForPreview} from 'react-devtools-shared/src/utils';
import {
cleanForBridge,
copyToClipboard,
Expand Down Expand Up @@ -3712,9 +3713,13 @@ export function attach(
cleanedInspectedElement.state,
createIsPathAllowed('state', null),
);
cleanedInspectedElement.ref = cleanForBridge(
cleanedInspectedElement.ref,
createIsPathAllowed('ref', null),

// Ref is a special case;
// don't dehydrate it in the same way (because it's not hydratable/inspectable).
// Just stringify it instead...
cleanedInspectedElement.ref = formatDataForPreview(
mostRecentlyInspectedElement.ref,
true,
);

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-shared/src/backendAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ export function convertInspectedElementBackendToFrontend(
props,
rendererPackageName,
rendererVersion,
ref,
rootType,
state,
key,
errors,
warnings,
ref,
} = inspectedElementBackend;

const inspectedElement: InspectedElementFrontend = {
Expand Down Expand Up @@ -259,9 +259,9 @@ export function convertInspectedElementBackendToFrontend(
hooks: hydrateHelper(hooks),
props: hydrateHelper(props),
state: hydrateHelper(state),
ref,
errors,
warnings,
ref: hydrateHelper(ref),
};

return inspectedElement;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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.
*
* @flow
*/

import * as React from 'react';
import sharedStyles from './InspectedElementSharedStyles.css';

// This is a little janky but it keeps the visual styles in sync.
import keyValueStyles from './KeyValue.css';

import type {InspectedElement} from './types';

type Props = {|
inspectedElement: InspectedElement,
|};

export default function InspectedElementSpecialPropsTree({
inspectedElement,
}: Props) {
const {ref} = inspectedElement;

if (ref === null) {
return null;
}

return (
<div className={sharedStyles.InspectedElementTree}>
{ref && (
<div className={keyValueStyles.Item} style={{paddingLeft: '1rem'}}>
ref
<div className={keyValueStyles.AfterName}>:</div>
<span className={keyValueStyles.Value}>{ref}</span>
</div>
)}
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import InspectedElementContextTree from './InspectedElementContextTree';
import InspectedElementErrorsAndWarningsTree from './InspectedElementErrorsAndWarningsTree';
import InspectedElementHooksTree from './InspectedElementHooksTree';
import InspectedElementPropsTree from './InspectedElementPropsTree';
import InspectedElementRefTree from './InspectedElementRefTree';
import InspectedElementSpecialPropsTree from './InspectedElementSpecialPropsTree';
import InspectedElementStateTree from './InspectedElementStateTree';
import InspectedElementStyleXPlugin from './InspectedElementStyleXPlugin';
import InspectedElementSuspenseToggle from './InspectedElementSuspenseToggle';
Expand Down Expand Up @@ -90,6 +90,13 @@ export default function InspectedElementView({
<div className={styles.InspectedElement}>
<HocBadges element={element} />

<InspectedElementSpecialPropsTree
bridge={bridge}
element={element}
inspectedElement={inspectedElement}
store={store}
/>

<InspectedElementPropsTree
bridge={bridge}
element={element}
Expand Down Expand Up @@ -143,13 +150,6 @@ export default function InspectedElementView({
store={store}
/>

<InspectedElementRefTree
bridge={bridge}
element={element}
inspectedElement={inspectedElement}
store={store}
/>

<NativeStyleEditor />

{showRenderedBy && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export type InspectedElement = {|
props: Object | null,
state: Object | null,
key: number | string | null,
ref: Object | Function | null,
ref: string | null,
errors: Array<[string, number]>,
warnings: Array<[string, number]>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export default function Refs() {
<div ref={() => {}} />
<ClassComponent ref={classComponentRef} />
<ForwardRefComponent ref={forwardRefComponentRef} />
<ClassComponent ref={() => {}} />
<ClassComponent
key={0}
ref={function namedRefCallback() {}}
foo="foo"
bar={123}
baz={function namedProp() {}}
/>
</React.Fragment>
);
}

0 comments on commit 3fc0c2e

Please sign in to comment.