React Simple Storage
A simple component and helper functions for using web storage with React.
You may also want to check out my related Hacker Noon article, How to take advantage of Local Storage in your React projects, for some context and logic behind this project.
Good use cases for react-simple-storage
- Persist and experiment with a component's state while developing.
- Save form data across user sessions.
- A simple, quick fake backend for a practice or portfolio project.
- More I can't think of...
Install
yarn add react-simple-storage
Using on IE11
For react-simple-storage to work on IE11, you'll need to use babel-polyfill.
yarn add babel-polyfill
Then import in your project.
import "babel-polyfill";
Usage
Component
Import and include an instance of react-simple-storage in a component whose state you want to save to web storage.
;; { superprops thisstate = text: "" } { return <div> // include the component somewhere in the parent to save the parent's state in web storage <SimpleStorage parent=this /> // the value of this input will be saved in web storage <input type="text" value=thisstatetext onChange= this /> </div> }
Props
Name | Type | Required? | Default | Description |
---|---|---|---|---|
parent | object | Yes | none | reference to the parent component, i.e. this |
prefix | string | No | "" | prefix added to storage keys to avoid name clashes across instances |
blacklist | array | No | [] | a list of parent component's state names/keys to ignore when saving to storage |
onParentStateHydrated | func | No | none | fires after the parent component's state has been updated with storage items. Basically a callback for working with the parent component's state once updated with storage. |
Helper Functions
clearStorage(prefix)
Clears items in storage
with the given prefix
, or all items if no prefix
is given.
prefix: String | optional
- Corresponds toprefix
prop passed to an instance of thereact-simple-storage
component.
Example
;; { superprops thisstate = text: "" } { return <div> // provide a prefix prop to be able to clear just the storage items // created by this instance of the react-simple-storage component <SimpleStorage parent=this prefix="ParentComponent" /> <input type="text" value=thisstatetext onChange= this /> // removes only storage items related to the ParentComponent <button onClick= > Clear storage for ParentComponent </button> // removes all items from storage <button onClick= > Clear all storage </button> </div> }
resetParentState(parent, initialState, keysToIgnore)
Resets the parent's state to given initialState
.
parent: Object | required
- Reference to the parent component, allowingreact-simple-storage
to access and update the parent component's state. If called within the parent component, simply passthis
.initialState: Object | required
- Thestate
of the parent component after the function executes.keysToIgnore: Array | optional
- A list of keys in the parent component'sstate
to ignore onresetParentState
. These pieces of that parent's state will NOT be reset.
Example
;; { superprops thisstate = text: "Initial Text" // store the component's initial state to reset it thisinitialState = thisstate; } { return <div> <SimpleStorage parent=this /> <input type="text" value=thisstatetext onChange= this /> // will set "text" in state to "Initial Text" <button onClick= > Reset parent state </button> // ignores "text" on reset, so will have no effect here <button onClick= > Do NOT reset text </button> </div> }
Built with
- store.js - Cross-browser storage for all use cases, used across the web.
License
This project is licensed under the MIT License - see the LICENSE.md file for details