-
Notifications
You must be signed in to change notification settings - Fork 49.1k
React DevTools: Show symbols used as keys in state #19786
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
Changes from 5 commits
5134885
ef7cdf5
71ab637
7caa608
f51b99a
de1bfae
db09127
dbf29c3
6b06f41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,16 +52,30 @@ const cachedDisplayNames: WeakMap<Function, string> = new WeakMap(); | |
// Try to reuse the already encoded strings. | ||
const encodedStringCache = new LRU({max: 1000}); | ||
|
||
export function alphaSortKeys(a: string, b: string): number { | ||
if (a > b) { | ||
export function alphaSortKeys( | ||
a: string | number | Symbol, | ||
b: string | number | Symbol, | ||
): number { | ||
if (a.toString() > b.toString()) { | ||
return 1; | ||
} else if (b > a) { | ||
} else if (b.toString() > a.toString()) { | ||
return -1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
export function getAllEnumerableKeys( | ||
obj: Object, | ||
): Array<string | number | Symbol> { | ||
const keys = []; | ||
for (const key in obj) { | ||
keys.push(key); | ||
} | ||
|
||
return keys.concat(Object.getOwnPropertySymbols(obj)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we wanted to not include any inherited keys (including symbols) we could just do: return [...Object.keys(object), ...Object.getOwnPropertySymbols(object)]; Or if we wanted to include both, I think we could: export function getAllEnumerableKeys(
object: Object,
): Array<string | number | Symbol> {
let keys = [];
let current = object;
while (current != null) {
const currentKeys = [
...Object.keys(current),
...Object.getOwnPropertySymbols(current),
];
const descriptors = Object.getOwnPropertyDescriptors(current);
for (let key of currentKeys) {
if (descriptors[key].enumerable) {
keys.push(key);
}
}
current = current.__proto__;
}
return keys;
} Which is best? I guess including inherited keys would be preferable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes @bvaughn you are right , I'm going to change it. Thank you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done :) |
||
|
||
export function getDisplayName( | ||
type: Function, | ||
fallbackName: string = 'Anonymous', | ||
|
@@ -657,15 +671,18 @@ export function formatDataForPreview( | |
return data.toString(); | ||
case 'object': | ||
if (showFormattedValue) { | ||
const keys = Object.keys(data).sort(alphaSortKeys); | ||
const keys = getAllEnumerableKeys(data).sort(alphaSortKeys); | ||
|
||
let formatted = ''; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
if (i > 0) { | ||
formatted += ', '; | ||
} | ||
formatted += `${key}: ${formatDataForPreview(data[key], false)}`; | ||
formatted += `${key.toString()}: ${formatDataForPreview( | ||
data[key], | ||
false, | ||
)}`; | ||
if (formatted.length > MAX_PREVIEW_STRING_LENGTH) { | ||
// Prevent doing a lot of unnecessary iteration... | ||
break; | ||
|
Uh oh!
There was an error while loading. Please reload this page.