Skip to content

Commit

Permalink
docs: interface fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomantovani committed Mar 7, 2021
1 parent 05f4297 commit d5779e7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ You can try this live version on: https://penseapp-uselocalstoragereduce.web.app
```tsx
import { useLocalStorageReducer } from "@penseapp/useLocalStorageReducer";

const [stateExample, dispatchExample] = useLocalStorageReducer(
export interface ExampleContextInterface {
field1: string;
}

export type ExampleAction = UPDATE_STATE; // Your reducers used in switch

const [stateExample, dispatchExample] = useLocalStorageReducer<
ExampleContextInterface,
ExampleAction
>(
"localStorage-key",
Reducer,
exampleInitialState,
Expand Down Expand Up @@ -71,7 +80,9 @@ Now you can reload your browser and your state will maintein

const App: React.FC = () => {
- const [state, setstate] = useReducer(reducer, initialState);
+ const [state, setstate] = useLocalStorageReducer<boolean>('keyName', reducer, initialState, false);
+ const [state, setstate] = useLocalStorageReducer<I, A>('keyName', reducer, initialState, false);
// I: Your object
// A: Your action

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged",
"pre-push": "yarn lint",
"pre-push": "yarn && yarn build:all && yarn lint",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
Expand Down
2 changes: 1 addition & 1 deletion playground/src/ExampleContext/ExampleReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "./ExampleInterface";
import { updateStateAction } from "./actions/updateStateAction";

function ExampleReducer(
export function ExampleReducer(
state: ExampleContextInterface,
action: ExampleAction
): ExampleContextInterface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import React, { Reducer, ReducerState } from "react";
export declare function useLocalStorageReducer<INTERFACE, ACTION>(
import { Reducer, ReducerState, Dispatch, ReducerAction } from "react";
export declare function useLocalStorageReducer<I, A>(
key: string,
reducer: Reducer<INTERFACE, React.Dispatch<ACTION>>,
initialState: INTERFACE,
reducer: Reducer<I, A>,
initialState: I,
expire?: number | boolean
): [
ReducerState<Reducer<INTERFACE, ACTION>>,
React.Dispatch<React.Dispatch<ACTION>>
];
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>];
export default useLocalStorageReducer;
10 changes: 5 additions & 5 deletions src/useLocalStorageReducer/useLocalStorageReducer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {
import {
useEffect,
useReducer,
Reducer,
Expand All @@ -8,12 +8,12 @@ import React, {
} from "react";
import ExpiredStorage from "expired-storage";

export function useLocalStorageReducer<INTERFACE, ACTION>(
export function useLocalStorageReducer<I, A>(
key: string,
reducer: Reducer<INTERFACE, React.Dispatch<ACTION>>,
initialState: INTERFACE,
reducer: Reducer<I, A>,
initialState: I,
expire: number | boolean = 60 * 30
): [ReducerState<R>, Dispatch<ReducerAction<R>>] {
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>] {
const [state, dispatch] = useReducer(
reducer,
initialState,
Expand Down

0 comments on commit d5779e7

Please sign in to comment.