Skip to content

Commit

Permalink
[APM] Fix for default fields in correlations view (#91868)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogupte committed Feb 19, 2021
1 parent 99a60ca commit c00cb8c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions x-pack/plugins/apm/public/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
* 2.0.
*/

import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } from 'react';

export function useLocalStorage<T>(key: string, defaultValue: T) {
const [item, setItem] = useState<T>(getFromStorage());

function getFromStorage() {
const getFromStorage = useCallback(() => {
const storedItem = window.localStorage.getItem(key);

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

return toStore;
}
}, [key, defaultValue]);

const [item, setItem] = useState<T>(getFromStorage());

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

useEffect(() => {
setItem(getFromStorage());
}, [getFromStorage]);

return [item, saveToStorage] as const;
}

0 comments on commit c00cb8c

Please sign in to comment.