An unified package for saving, reading and deleting data from/to sessionStorage, localStorage or cookies. If user's device doesn't support sessionStorage and/or localStorage, the package uses cookies as a fallback. Note it's designed for browsers only.
npm install --save react-device-storage
Enables using of cookies.
Returns this
, so you can manipulate with data directly.
Enables using of localStorage.
Returns this
, so you can manipulate with data directly.
Enables using of sessionStorage.
Returns this
, so you can manipulate with data directly.
Saves data in selected storage. key
should be a 'string', value may be whatever you want to store.
Reads data from selected storage.
Deletes data from selected storage.
Enable or disable cookies fallback (boolean)
Object of settings for cookies
Cookie path
Use/
as the path if you want your cookie to be accessible on all pages.
[default/
]
Relative max age of the cookie from when the client receives it (seconds) [default
2592000
(30 days)]
Domain for the cookie
Usehttps://*.yourdomain.com
if you want to access the cookie in all your subdomains.
Is only accessible through HTTPS? (boolean)
import { Component } from 'react';
import DeviceStorage from 'react-device-storage';
import HelloWorld from './HelloWorld';
export default class MyApp extends Component {
construct(props) {
super(props);
this.storage = new DeviceStorage({
cookieFallback: true,
cookie: {
secure: true
}
}).localStorage();
this.state = {
name: this.storage.read('name')
};
}
setName() {
let name = 'User Name';
this.storage.save('name', name);
this.setState({
name
});
}
render() {
return (
<HelloWorld userName={this.state.name} onClick={this.setName.bind(this)} />
);
}
}
This code is released under the MIT license. Please see LICENSE file for details.
Reporting of any issues are appreciated.
- Social Identity & Login Aggregator: Ukey1
- Ukey1 SDK for React: ukey1-react-sdk