Last active
January 31, 2019 10:27
-
-
Save fnky/6d27114fefad398fffbe90ec8bbdb101 to your computer and use it in GitHub Desktop.
A React Hook to set persisted state, similar to useState
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import usePersistedState from './usePersistedState'; | |
function App() { | |
const [count, setCount, unsetCount] = usePersistedState( | |
0, | |
'count', | |
sessionStorage | |
); | |
return ( | |
<div className="App"> | |
<p>You have clicked {count} times</p> | |
<button onClick={() => setCount(prevCount => prevCount + 1)}> | |
Click | |
</button> | |
<button onClick={() => unsetCount()}>Unset</button> | |
</div> | |
); | |
} | |
const rootElement = document.getElementById('root'); | |
ReactDOM.render(<App />, rootElement); |
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
import { useState } from 'react'; | |
export default function usePersistedState<T>( | |
initialState: T, | |
key: string, | |
storage: Storage = localStorage | |
): [T, (updater: T | ((previousState: T) => T)) => void, () => void] { | |
let cachedValue = localStorage.getItem(key); | |
let value: T; | |
if (!cachedValue) { | |
value = initialState; | |
} else { | |
value = JSON.parse(cachedValue); | |
} | |
const [state, setState] = useState(value); | |
const setPersistedState = (nextState: T | ((previousState: T) => T)) => { | |
if (typeof nextState === 'function') { | |
return setState(previousState => { | |
const newState = nextState(previousState); | |
localStorage.setItem(key, JSON.stringify(newState)); | |
return newState; | |
}); | |
} | |
localStorage.setItem(key, JSON.stringify(nextState)); | |
setState(nextState); | |
}; | |
const unsetPersistedState = () => { | |
localStorage.removeItem(key); | |
}; | |
return [state, setPersistedState, unsetPersistedState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment