Skip to main content

Posts

Showing posts with the label React-LocalStorage

React-LocalStorage

Simply synchronize a component's state with localStorage, when available. Usage A simple component: var React = require('react'); var LocalStorageMixin = require('react-localstorage'); var TestComponent = module.exports = React.createClass({ displayName: 'TestComponent', // This is all you need to do mixins: [LocalStorageMixin], getInitialState: function() { return {counter: 0}; }, onClick: function() { this.setState({counter: this.state.counter + 1}); }, render: function() { return ( <span onClick={this.onClick}>{this.state.counter}</span> ); } }); Options The key that state is serialized to under localStorage is chosen with the following code: function getLocalStorageKey(component) { if (component.getLocalStorageKey) return component.getLocalStorageKey(); if (component.props.localStorageKey === false) return false; if (typeof component.props.localStor