Skip to content

Commit e40cd3f

Browse files
committed
Update Readme.md
1 parent cc2bb57 commit e40cd3f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Readme.md

+31
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@ store.dispatch(localStorage.setItem('todos.0', 'Clean the kitchen'))
4242

4343
The API of the action creators is identical to that of the native localStorage object, with the sole exception that the property `length` has been changed to the function `getLength`.
4444

45+
## Example - Auth token getter/setter
46+
47+
Getting/setting an auth token:
48+
49+
```javascript
50+
function authToken (newToken) {
51+
return arguments.length > 0
52+
? localStorage.setItem('authToken', newToken)
53+
: localStorage.getItem('authToken')
54+
}
55+
56+
function setUserToken (token) {
57+
return {type: 'SET_USER_TOKEN', payload: token}
58+
}
59+
60+
function reducer (state, action) {
61+
if (action.type === 'SET_USER_TOKEN') {
62+
return {
63+
...state,
64+
authToken: action.payload
65+
}
66+
}
67+
}
68+
69+
function checkUserStatus () {
70+
store
71+
.dispatch(authToken())
72+
.then(token => store.dispatch(setUserToken(token))
73+
}
74+
```
75+
4576
## Use with sessionStorage
4677
4778
Each of the action creators takes another parameter, `STORAGE_TYPE`. You can access this from `STORAGE_TYPE` exported by this module. Values are `local` (the default) and `session`, which you can use if you want the actions to be applied to session storage rather than local storage.

0 commit comments

Comments
 (0)