Skip to content

Commit bec0733

Browse files
author
Brian Vaughn
committed
Fix hooks warnings identified by prerelease version of react-hooks ESLint plugin
1 parent a314150 commit bec0733

8 files changed

Lines changed: 72 additions & 38 deletions

File tree

shells/dev/app/ToDoList/List.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function List(props: Props) {
3434
setUID(uid + 1);
3535
setNewItemText('');
3636
}
37-
}, [newItemText]);
37+
}, [newItemText, items, uid]);
3838

3939
const handleKeyPress = useCallback(
4040
event => {

src/devtools/views/Element.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,35 @@ export default function ElementView({ index, style }: Props) {
2525

2626
const element = getElementAtIndex(index);
2727

28-
if (element == null) {
29-
console.warn(`<ElementView> Could not find element at index ${index}`);
30-
return null;
31-
}
32-
33-
const { depth, displayName, id, key, type } = ((element: any): Element);
28+
const id = element === null ? null : element.id;
3429

35-
const handleDoubleClick = useCallback(() => selectOwner(id), [id]);
30+
const handleDoubleClick = useCallback(() => {
31+
if (id !== null) {
32+
selectOwner(id);
33+
}
34+
}, [id, selectOwner]);
3635

3736
// TODO Add click and key handlers for toggling element open/close state.
3837

3938
const handleClick = useCallback(
40-
({ metaKey }) => selectElementByID(metaKey ? null : id),
41-
[id]
39+
({ metaKey }) => {
40+
if (id !== null) {
41+
selectElementByID(metaKey ? null : id);
42+
}
43+
},
44+
[id, selectElementByID]
4245
);
4346

47+
// Handle elements that are removed from the tree while an async render is in progress.
48+
if (element == null) {
49+
console.warn(`<ElementView> Could not find element at index ${index}`);
50+
51+
// This return needs to happen after hooks, since hooks can't be conditional.
52+
return null;
53+
}
54+
55+
const { depth, displayName, key, type } = ((element: any): Element);
56+
4457
const isSelected = selectedElementID === id;
4558
const showDollarR =
4659
isSelected && (type === ElementTypeClass || type === ElementTypeFunction);
@@ -58,7 +71,7 @@ export default function ElementView({ index, style }: Props) {
5871
}}
5972
>
6073
<span className={styles.Component}>
61-
<DisplayName displayName={displayName} id={id} />
74+
<DisplayName displayName={displayName} id={((id: any): number)} />
6275
{key && (
6376
<Fragment>
6477
&nbsp;<span className={styles.AttributeName}>key</span>=

src/devtools/views/InspectedElementTree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function EditableValue({
183183
setEditableValue(target.value);
184184
}
185185
},
186-
[dataType, setEditableValue]
186+
[dataType, overrideValueFn, path]
187187
);
188188

189189
const handleKeyPress = useCallback(

src/devtools/views/SelectedElement.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default function SelectedElement(_: Props) {
4343
});
4444
}
4545
}
46-
}, [bridge, selectedElementID, store]);
46+
}, [bridge, element, selectedElementID, store]);
4747

4848
// TODO Make "view source" button work
4949

@@ -176,7 +176,10 @@ function InspectedElementView({
176176
function OwnerView({ displayName, id }: { displayName: string, id: number }) {
177177
const { selectElementByID } = useContext(TreeContext);
178178

179-
const handleClick = useCallback(() => selectElementByID(id), [id]);
179+
const handleClick = useCallback(() => selectElementByID(id), [
180+
id,
181+
selectElementByID,
182+
]);
180183

181184
return (
182185
<div
@@ -260,7 +263,7 @@ function useInspectedElement(id: number | null): InspectedElement | null {
260263

261264
bridge.removeListener('inspectedElement', onInspectedElement);
262265
};
263-
}, [id]);
266+
}, [bridge, id, idRef, store]);
264267

265268
return inspectedElement;
266269
}

src/devtools/views/SettingsContext.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function SettingsContextController({ browserTheme, children }: Props) {
7676
default:
7777
throw Error(`Unsupported theme value "${theme}"`);
7878
}
79-
}, [theme]);
79+
}, [browserTheme, theme]);
8080

8181
const value = useMemo(
8282
() => ({
@@ -89,7 +89,14 @@ function SettingsContextController({ browserTheme, children }: Props) {
8989
? compactLineHeight
9090
: comfortableLineHeight,
9191
}),
92-
[displayDensity, setDisplayDensity, theme, setTheme]
92+
[
93+
comfortableLineHeight,
94+
compactLineHeight,
95+
displayDensity,
96+
setDisplayDensity,
97+
setTheme,
98+
theme,
99+
]
93100
);
94101

95102
return (

src/devtools/views/Tree.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ export default function Tree(props: Props) {
6363
return () => {
6464
window.removeEventListener('keydown', handleKeyDown);
6565
};
66-
}, [
67-
selectedElementIndex,
68-
selectNextElementInTree,
69-
selectPreviousElementInTree,
70-
]);
66+
}, [selectNextElementInTree, selectPreviousElementInTree]);
7167

7268
// Let react-window know to re-render any time the underlying tree data changes.
7369
// This includes the owner context, since it controls a filtered view of the tree.

src/devtools/views/TreeContext.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,19 @@ function TreeContextController({ children }: {| children: React$Node |}) {
595595
resetOwnerStack,
596596
selectOwner,
597597
}),
598-
[state]
598+
[
599+
getElementAtIndex,
600+
goToNextSearchResult,
601+
goToPreviousSearchResult,
602+
resetOwnerStack,
603+
selectElementAtIndex,
604+
selectElementByID,
605+
selectNextElementInTree,
606+
selectOwner,
607+
selectPreviousElementInTree,
608+
setSearchText,
609+
state,
610+
]
599611
);
600612

601613
// Listen for host element selections.
@@ -632,7 +644,7 @@ function TreeContextController({ children }: {| children: React$Node |}) {
632644
store.addListener('mutated', handleStoreMutated);
633645

634646
return () => store.removeListener('mutated', handleStoreMutated);
635-
}, [dispatch, store]);
647+
}, [dispatch, initialRevision, store]);
636648

637649
return <TreeContext.Provider value={value}>{children}</TreeContext.Provider>;
638650
}

src/devtools/views/hooks.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
// @flow
22

3-
import { useLayoutEffect, useState } from 'react';
3+
import { useCallback, useLayoutEffect, useState } from 'react';
44

55
// Forked from https://usehooks.com/useLocalStorage/
66
export function useLocalStorage<T>(
77
key: string,
88
initialValue: T
99
): [T, (value: T | (() => T)) => void] {
10-
const getValueFromLocalStorage = () => {
10+
const getValueFromLocalStorage = useCallback(() => {
1111
try {
1212
const item = window.localStorage.getItem(key);
1313
return item ? JSON.parse(item) : initialValue;
1414
} catch (error) {
1515
console.log(error);
1616
return initialValue;
1717
}
18-
};
18+
}, [initialValue, key]);
1919

2020
const [storedValue, setStoredValue] = useState(getValueFromLocalStorage);
2121

22-
const setValue = value => {
23-
try {
24-
const valueToStore =
25-
value instanceof Function ? value(storedValue) : value;
26-
setStoredValue(valueToStore);
27-
window.localStorage.setItem(key, JSON.stringify(valueToStore));
28-
} catch (error) {
29-
console.log(error);
30-
}
31-
};
22+
const setValue = useCallback(
23+
value => {
24+
try {
25+
const valueToStore =
26+
value instanceof Function ? value(storedValue) : value;
27+
setStoredValue(valueToStore);
28+
window.localStorage.setItem(key, JSON.stringify(valueToStore));
29+
} catch (error) {
30+
console.log(error);
31+
}
32+
},
33+
[key, storedValue]
34+
);
3235

3336
// Listen for changes to this local storage value made from other windows.
3437
// This enables the e.g. "⚛️ Elements" tab to update in response to changes from "⚛️ Settings".
@@ -45,7 +48,7 @@ export function useLocalStorage<T>(
4548
return () => {
4649
window.removeEventListener('storage', onStorage);
4750
};
48-
}, [key, storedValue]);
51+
}, [getValueFromLocalStorage, key, storedValue, setValue]);
4952

5053
return [storedValue, setValue];
5154
}

0 commit comments

Comments
 (0)