-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add useSessionStorage * chore: uncomment session storage docs story
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# `useSessionStorage` | ||
|
||
React side-effect hook that manages a single `sessionStorage` key. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useSessionStorage} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [value, setValue] = useSessionStorage('my-key', 'foo'); | ||
|
||
return ( | ||
<div> | ||
<div>Value: {value}</div> | ||
<button onClick={() => setValue('bar')}>bar</button> | ||
<button onClick={() => setValue('baz')}>baz</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
|
||
## Reference | ||
|
||
```js | ||
useSessionStorage(key); | ||
useSessionStorage(key, initialValue); | ||
useSessionStorage(key, initialValue, raw); | ||
``` | ||
|
||
- `key` — `sessionStorage` key to manage. | ||
- `initialValue` — initial value to set, if value in `sessionStorage` is empty. | ||
- `raw` — boolean, if set to `true`, hook will not attempt to JSON serialize stored values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useSessionStorage} from '..'; | ||
|
||
const Demo = () => { | ||
const [value, setValue] = useSessionStorage('hello-key', 'foo'); | ||
|
||
return ( | ||
<div> | ||
<div>Value: {value}</div> | ||
<button onClick={() => setValue('bar')}>bar</button> | ||
<button onClick={() => setValue('baz')}>baz</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('useSessionStorage', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useSessionStorage.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useState, useEffect} from './react'; | ||
|
||
const isClient = typeof window === 'object'; | ||
|
||
const useSessionStorage = <T>(key: string, initialValue?: T, raw?: boolean): [T, (value: T) => void] => { | ||
if (!isClient) { | ||
return [initialValue as T, () => {}]; | ||
} | ||
|
||
const [state, setState] = useState<T>(() => { | ||
try { | ||
const sessionStorageValue = sessionStorage.getItem(key); | ||
if (typeof sessionStorageValue !== 'string') { | ||
sessionStorage.setItem(key, raw ? String(initialValue) : JSON.stringify(initialValue)); | ||
return initialValue; | ||
} else { | ||
return raw ? sessionStorageValue : JSON.parse(sessionStorageValue || 'null'); | ||
} | ||
} catch { | ||
// If user is in private mode or has storage restriction | ||
// sessionStorage can throw. JSON.parse and JSON.stringify | ||
// cat throw, too. | ||
return initialValue; | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
try { | ||
const serializedState = raw ? String(state) : JSON.stringify(state); | ||
sessionStorage.setItem(key, serializedState); | ||
} catch { | ||
// If user is in private mode or has storage restriction | ||
// sessionStorage can throw. Also JSON.stringify can throw. | ||
} | ||
}); | ||
|
||
return [state, setState]; | ||
}; | ||
|
||
export default useSessionStorage; |