Skip to content

Commit c00cb8c

Browse files
committed
[APM] Fix for default fields in correlations view (#91868)
1 parent 99a60ca commit c00cb8c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

x-pack/plugins/apm/public/hooks/useLocalStorage.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* 2.0.
66
*/
77

8-
import { useState, useEffect } from 'react';
8+
import { useState, useEffect, useCallback } from 'react';
99

1010
export function useLocalStorage<T>(key: string, defaultValue: T) {
11-
const [item, setItem] = useState<T>(getFromStorage());
12-
13-
function getFromStorage() {
11+
const getFromStorage = useCallback(() => {
1412
const storedItem = window.localStorage.getItem(key);
1513

1614
let toStore: T = defaultValue;
@@ -26,7 +24,9 @@ export function useLocalStorage<T>(key: string, defaultValue: T) {
2624
}
2725

2826
return toStore;
29-
}
27+
}, [key, defaultValue]);
28+
29+
const [item, setItem] = useState<T>(getFromStorage());
3030

3131
const updateFromStorage = () => {
3232
const storedItem = getFromStorage();
@@ -51,5 +51,9 @@ export function useLocalStorage<T>(key: string, defaultValue: T) {
5151
// eslint-disable-next-line react-hooks/exhaustive-deps
5252
}, []);
5353

54+
useEffect(() => {
55+
setItem(getFromStorage());
56+
}, [getFromStorage]);
57+
5458
return [item, saveToStorage] as const;
5559
}

0 commit comments

Comments
 (0)