Skip to content

Commit 05f4297

Browse files
committed
feat: wip
1 parent 37daaf7 commit 05f4297

File tree

6 files changed

+20
-24
lines changed

6 files changed

+20
-24
lines changed

playground/src/ExampleContext/ExampleContext.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ export const ExampleContext = createContext<ExampleProviderInterface>({
1717
});
1818

1919
const ExampleProvider: React.FC = ({ children }) => {
20-
const [stateExample, dispatchExample] = useLocalStorageReducer(
21-
"localStorage-key",
22-
Reducer,
23-
exampleInitialState,
24-
60 * 60
25-
);
20+
const [stateExample, dispatchExample] = useLocalStorageReducer<
21+
ExampleContextInterface,
22+
ExampleAction
23+
>("localStorage-key", Reducer, exampleInitialState, 60 * 60);
2624

2725
const globals = {
2826
stateExample,

playground/src/ExampleContext/ExampleReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from "./ExampleInterface";
66
import { updateStateAction } from "./actions/updateStateAction";
77

8-
function updateStateReducer(
8+
function ExampleReducer(
99
state: ExampleContextInterface,
1010
action: ExampleAction
1111
): ExampleContextInterface {
@@ -18,4 +18,4 @@ function updateStateReducer(
1818
}
1919
}
2020

21-
export default updateStateReducer;
21+
export default ExampleReducer;

playground/src/ExampleContext/actions/updateStateAction.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("File: ExampleContext", () => {
99
scenario,
1010
]);
1111

12-
it.each(eachArr)("Scenario: %s ", async (_, scenario) => {
12+
it.each(eachArr)("Scenario: %s", async (_, scenario) => {
1313
if (typeof scenario === "string") return;
1414

1515
const result = updateStateAction(scenario.state, {

playground/src/component-lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function useLocalStorageReducer(key, reducer, initialState, expire) {
1111
var expiredStorage = new ExpiredStorage();
1212
// Get from local storage by key
1313
var item = expiredStorage.getItem(key);
14+
var parsedInitialValue = JSON.stringify(initialState);
1415
// Parse stored json or if none return initialValue
15-
return item ? JSON.parse(item) : JSON.parse(initialState);
16+
return item ? JSON.parse(item) : JSON.parse(parsedInitialValue);
1617
} catch (error) {
1718
// If error also return initialValue
1819
console.error(error);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Reducer, ReducerState, Dispatch, ReducerAction } from "react";
2-
export declare function useLocalStorageReducer<I>(
1+
import React, { Reducer, ReducerState } from "react";
2+
export declare function useLocalStorageReducer<INTERFACE, ACTION>(
33
key: string,
4-
reducer: Reducer<any, any>,
5-
initialState: I,
4+
reducer: Reducer<INTERFACE, React.Dispatch<ACTION>>,
5+
initialState: INTERFACE,
66
expire?: number | boolean
77
): [
8-
ReducerState<Reducer<any, any>>,
9-
Dispatch<ReducerAction<Reducer<any, any>>>
8+
ReducerState<Reducer<INTERFACE, ACTION>>,
9+
React.Dispatch<React.Dispatch<ACTION>>
1010
];
1111
export default useLocalStorageReducer;

src/useLocalStorageReducer/useLocalStorageReducer.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import React, {
22
useEffect,
33
useReducer,
44
Reducer,
@@ -8,15 +8,12 @@ import {
88
} from "react";
99
import ExpiredStorage from "expired-storage";
1010

11-
export function useLocalStorageReducer<I>(
11+
export function useLocalStorageReducer<INTERFACE, ACTION>(
1212
key: string,
13-
reducer: Reducer<unknown, unknown>,
14-
initialState: I,
13+
reducer: Reducer<INTERFACE, React.Dispatch<ACTION>>,
14+
initialState: INTERFACE,
1515
expire: number | boolean = 60 * 30
16-
): [
17-
ReducerState<Reducer<unknown, unknown>>,
18-
Dispatch<ReducerAction<Reducer<unknown, unknown>>>
19-
] {
16+
): [ReducerState<R>, Dispatch<ReducerAction<R>>] {
2017
const [state, dispatch] = useReducer(
2118
reducer,
2219
initialState,

0 commit comments

Comments
 (0)