generated from penseapp/useLocalStorage
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from gritzkoo/master
Adjust build steps to complience to npm
- Loading branch information
Showing
13 changed files
with
8,794 additions
and
3,144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export { useLocalStorageReducer } from "./useLocalStorageReducer"; | ||
//# sourceMappingURL=index.d.ts.map | ||
import { Reducer, ReducerState, Dispatch, ReducerAction } from "react"; | ||
export declare function useLocalStorageReducer<I, A>( | ||
key: string, | ||
reducer: Reducer<I, A>, | ||
initialState: I, | ||
expire?: number | boolean | ||
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>]; | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
playground/src/component-lib/useLocalStorageReducer/index.d.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
playground/src/component-lib/useLocalStorageReducer/index.d.ts.map
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
playground/src/component-lib/useLocalStorageReducer/useLocalStorageReducer.d.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
playground/src/component-lib/useLocalStorageReducer/useLocalStorageReducer.d.ts.map
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
export { useLocalStorageReducer } from "./useLocalStorageReducer"; | ||
import { | ||
useEffect, | ||
useReducer, | ||
Reducer, | ||
ReducerState, | ||
Dispatch, | ||
ReducerAction, | ||
} from "react"; | ||
import ExpiredStorage from "expired-storage"; | ||
|
||
export function useLocalStorageReducer<I, A>( | ||
key: string, | ||
reducer: Reducer<I, A>, | ||
initialState: I, | ||
expire: number | boolean = 60 * 30 | ||
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>] { | ||
const [state, dispatch] = useReducer( | ||
reducer, | ||
initialState, | ||
(initialState) => { | ||
try { | ||
const expiredStorage = new ExpiredStorage(); | ||
|
||
// Get from local storage by key | ||
const item = expiredStorage.getItem(key); | ||
|
||
const parsedInitialValue = JSON.stringify(initialState); | ||
|
||
// Parse stored json or if none return initialValue | ||
return item ? JSON.parse(item) : JSON.parse(parsedInitialValue); | ||
} catch (error) { | ||
// If error also return initialValue | ||
console.error(error); | ||
return initialState; | ||
} | ||
} | ||
); | ||
|
||
useEffect(() => { | ||
try { | ||
const expiredStorage = new ExpiredStorage(); | ||
|
||
expire !== false && typeof expire === "number" | ||
? expiredStorage.setItem(key, JSON.stringify(state), expire) | ||
: window.localStorage.setItem(key, JSON.stringify(state)); | ||
} catch (error) { | ||
// TODO: A more advanced implementation would handle the error case | ||
console.error(error); | ||
} | ||
|
||
localStorage.setItem(key, JSON.stringify(state)); | ||
}, [expire, key, state]); | ||
|
||
return [state, dispatch]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.