13
13
<!--  -->
14
14
<!-- [](https://coveralls.io/github/penseapp/useLocalStorageReducer?branch=master) -->
15
15
16
- <!-- </div> --> !
17
- [ Peek 2021-03-06 15-36] ( https://user-images.githubusercontent.com/5152197/110217257-d131ed80-7e91-11eb-8f57-cb23404c52d6.gif )
18
-
16
+ <!-- </div> -->
19
17
20
18
This is a react hook that allows you to use the power of browser localstorage
21
- and the useState react hook combined!
19
+ and the useReducer react hook combined!
22
20
23
21
The API is the same, and you'll see no difference between them!
24
22
25
23
## Example live action
26
24
27
- ![ Peek 2021-02-28 17-40 ] ( https://user-images.githubusercontent.com/5152197/109432913-444be780-79ec -11eb-87ad-bcc3d1204bb8 .gif )
25
+ ![ Peek 2021-03-06 15-36 ] ( https://user-images.githubusercontent.com/5152197/110217257-d131ed80-7e91 -11eb-8f57-cb23404c52d6 .gif )
28
26
29
- You can try this live version on: https://penseapp-useLocalStorageReducer .web.app/
27
+ You can try this live version on: https://penseapp-uselocalstoragereduce .web.app/
30
28
31
29
## TL;DR
32
30
33
31
``` tsx
34
32
import { useLocalStorageReducer } from " @penseapp/useLocalStorageReducer" ;
35
33
36
- const [state, setState] = useLocalStorageReducer <string >(
37
- " keyName" ,
38
- " useLocalStorageReducer" ,
39
- false // or a number
34
+ const [stateExample, dispatchExample] = useLocalStorageReducer (
35
+ " localStorage-key" ,
36
+ Reducer ,
37
+ exampleInitialState ,
38
+ 60 * 60 // 1 hour
40
39
);
41
40
```
42
41
@@ -54,22 +53,25 @@ npm i @penseapp/useLocalStorageReducer
54
53
55
54
## How to use
56
55
57
- Import the lib on your component
56
+ You need to implement the reducer/provider on your side. If you want a suggestion to
57
+ organize the useLocalStorageReducer, please check the files inside the folder
58
+ ` playground/src/ExampleContext ` . And look at the file ` ExampleContext.tsx `
58
59
59
60
``` tsx
60
61
import { useLocalStorageReducer } from " @penseapp/useLocalStorageReducer" ;
61
62
```
62
63
63
- Simple change the ` useState ` to ` useLocalStorageReducer ` on any hooks and it's done.
64
+ Simple change the ` useReducer ` to ` useLocalStorageReducer ` on any hooks and it's done.
64
65
Now you can reload your browser and your state will maintein
65
66
66
67
``` diff
67
- import React, { useState } from "react";
68
- import { useLocalStorageReducer } from "@penseapp/useLocalStorageReducer";
68
+ - import React, { useReducer } from "react";
69
+ + import React from "react";
70
+ + import { useLocalStorageReducer } from "@penseapp/useLocalStorageReducer";
69
71
70
72
const App: React.FC = () => {
71
- - const [state, setstate] = useState<boolean>(false );
72
- + const [state, setstate] = useLocalStorageReducer<boolean>('keyName', false);
73
+ - const [state, setstate] = useReducer(reducer, initialState );
74
+ + const [state, setstate] = useLocalStorageReducer<boolean>('keyName', reducer, initialState, false);
73
75
74
76
return (
75
77
<>
@@ -83,11 +85,14 @@ export default App;
83
85
84
86
## API / Props
85
87
86
- | Name | Type | Required | Default | Description |
87
- | ------------ | --------------- | -------- | ------------------ | ----------------------------------------------- |
88
- | key | string | true | | Key name from ` localStorage ` (Should be unique) |
89
- | initialValue | any (Generic) | true | | Same as the ` useState ` hook |
90
- | expire | number or false | false | 60 \* 30 (seconds) | Time in seconds to expiry (false to infinite) |
88
+ | Name | Type | Required | Default | Description |
89
+ | ------------ | --------------- | -------- | ------------------ | -------------------------------------------------------- |
90
+ | key | string | true | | Key name from ` localStorage ` (Should be unique) |
91
+ | reducer | React Reducer | true | | https://reactjs.org/docs/hooks-reference.html#usereducer |
92
+ | initialValue | any (Generic) | true | | Same as the ` useReducer ` hook |
93
+ | expire | number or false | false | 60 \* 30 (seconds) | Time in seconds to expiry (false to infinite) |
94
+
95
+ <br />
91
96
92
97
## localStorage expire
93
98
@@ -98,34 +103,42 @@ You have two options:
98
103
- Set the time in seconds
99
104
- Set false to infinite
100
105
106
+ <br />
107
+
101
108
** Examples**
102
109
103
110
``` tsx
111
+ import Reducer from " your-reducer" ;
112
+
104
113
// Never expires (infinite)
105
114
const [state, setstate] = useLocalStorageReducer <boolean >(
106
115
" keyName" ,
107
- " defaultValue" ,
116
+ Reducer ,
117
+ { field1: " example" },
108
118
false
109
119
);
110
120
111
121
// Expires in 1 minute
112
122
const [state, setstate] = useLocalStorageReducer <boolean >(
113
123
" keyName" ,
114
- " defaultValue" ,
124
+ Reducer ,
125
+ { field1: " example" },
115
126
60
116
127
);
117
128
118
129
// Expires in 1 hour
119
130
const [state, setstate] = useLocalStorageReducer <boolean >(
120
131
" keyName" ,
121
- " defaultValue" ,
132
+ Reducer ,
133
+ { field1: " example" },
122
134
60 * 60 * 1
123
135
); // 3600 seconds
124
136
125
137
// Expires in 12 hours
126
138
const [state, setstate] = useLocalStorageReducer <boolean >(
127
139
" keyName" ,
128
- " defaultValue" ,
140
+ Reducer ,
141
+ { field1: " example" },
129
142
60 * 60 * 12
130
143
); // 43200 seconds
131
144
```
0 commit comments