Skip to content

Commit 7a74abe

Browse files
committed
docs: readme
1 parent 132b01e commit 7a74abe

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

README.md

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,29 @@
1313
<!-- ![GitHub Workflow Status (event)](https://img.shields.io/github/workflow/status/@penseapp/useLocalStorageReducer/test) -->
1414
<!-- [![Coverage Status](https://coveralls.io/repos/github/penseapp/useLocalStorageReducer/badge.svg?branch=master)](https://coveralls.io/github/penseapp/useLocalStorageReducer?branch=master) -->
1515

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> -->
1917

2018
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!
2220

2321
The API is the same, and you'll see no difference between them!
2422

2523
## Example live action
2624

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)
2826

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/
3028

3129
## TL;DR
3230

3331
```tsx
3432
import { useLocalStorageReducer } from "@penseapp/useLocalStorageReducer";
3533

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
4039
);
4140
```
4241

@@ -54,22 +53,25 @@ npm i @penseapp/useLocalStorageReducer
5453

5554
## How to use
5655

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`
5859

5960
```tsx
6061
import { useLocalStorageReducer } from "@penseapp/useLocalStorageReducer";
6162
```
6263

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.
6465
Now you can reload your browser and your state will maintein
6566

6667
```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";
6971

7072
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);
7375

7476
return (
7577
<>
@@ -83,11 +85,14 @@ export default App;
8385

8486
## API / Props
8587

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 />
9196

9297
## localStorage expire
9398

@@ -98,34 +103,42 @@ You have two options:
98103
- Set the time in seconds
99104
- Set false to infinite
100105

106+
<br />
107+
101108
**Examples**
102109

103110
```tsx
111+
import Reducer from "your-reducer";
112+
104113
// Never expires (infinite)
105114
const [state, setstate] = useLocalStorageReducer<boolean>(
106115
"keyName",
107-
"defaultValue",
116+
Reducer,
117+
{ field1: "example" },
108118
false
109119
);
110120

111121
// Expires in 1 minute
112122
const [state, setstate] = useLocalStorageReducer<boolean>(
113123
"keyName",
114-
"defaultValue",
124+
Reducer,
125+
{ field1: "example" },
115126
60
116127
);
117128

118129
// Expires in 1 hour
119130
const [state, setstate] = useLocalStorageReducer<boolean>(
120131
"keyName",
121-
"defaultValue",
132+
Reducer,
133+
{ field1: "example" },
122134
60 * 60 * 1
123135
); // 3600 seconds
124136

125137
// Expires in 12 hours
126138
const [state, setstate] = useLocalStorageReducer<boolean>(
127139
"keyName",
128-
"defaultValue",
140+
Reducer,
141+
{ field1: "example" },
129142
60 * 60 * 12
130143
); // 43200 seconds
131144
```

0 commit comments

Comments
 (0)