Skip to content

Commit 1c3a4b3

Browse files
committed
Use overloaded function for getFromLocalStorage type
This allows clients to not provide a default value, in which case undefined is returned they must explicitly handle it. Also move a CSS import from App.tsx to index.tsx
1 parent b346a23 commit 1c3a4b3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "currency-flags/dist/currency-flags.css";
21
import { FunctionComponent, useEffect, useReducer, useState } from "react";
32
import { BalanceContainer } from "./components/BalanceContainer";
43
import { ShortcutsContainer } from "./components/ShortcutsContainer";

src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { StrictMode } from "react";
22
import { render } from "react-dom";
3-
import "./index.scss";
43
import { App } from "./App";
54

5+
import "./index.scss";
6+
import "currency-flags/dist/currency-flags.css";
7+
68
render(
79
<StrictMode>
810
<App />

src/util/LocalStorageHelper.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ export enum LocalStorageKey {
55
DISPLAYED_BALANCES = "displayedBalances",
66
}
77

8-
export const getFromLocalStorage = (key: LocalStorageKey, defaultValue: string | (() => string) = ""): string => {
8+
export function getFromLocalStorage(key: LocalStorageKey): string | undefined;
9+
export function getFromLocalStorage(key: LocalStorageKey, defaultValue: string): string;
10+
export function getFromLocalStorage(key: LocalStorageKey, defaultValue?: string): string | undefined {
911
let value = localStorage.getItem(key);
10-
if (value === null) {
11-
if (typeof defaultValue === "string") {
12-
return defaultValue;
13-
}
14-
return defaultValue();
12+
if (value) {
13+
return value;
1514
}
16-
return value;
17-
};
15+
16+
if (typeof defaultValue === "string") {
17+
return defaultValue;
18+
}
19+
20+
return undefined;
21+
}
1822

1923
export const setLocalStorage = (key: LocalStorageKey, value: string) => {
2024
localStorage.setItem(key, value);

0 commit comments

Comments
 (0)