Skip to content

Instantly share code, notes, and snippets.

@suzdalnitski
Created August 12, 2018 21:31
Show Gist options
  • Save suzdalnitski/5ad107cca157828c9a49f0e357d41a4d to your computer and use it in GitHub Desktop.
Save suzdalnitski/5ad107cca157828c9a49f0e357d41a4d to your computer and use it in GitHub Desktop.
Simple HOC
const withLocalStorage = (WrappedComponent) => {
const loadFromStorage = (key) => localStorage.getItem(key);
const saveToStorage = (key, value) => localStorage.setItem(key, value);
const removeFromStorage = (key) => localStorage.removeItem(key);
return (props) => (
<WrappedComponent
loadFromStorage={loadFromStorage}
saveToStorage={saveToStorage}
removeFromStorage={removeFromStorage}
{...props}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment