import React from "react"; import RGL, { WidthProvider } from "react-grid-layout"; const ReactGridLayout = WidthProvider(RGL); const originalLayout = getFromLS("layout") || []; /** * This layout demonstrates how to sync to localstorage. */ export default class LocalStorageLayout extends React.PureComponent { static defaultProps = { className: "layout", cols: 12, rowHeight: 30, onLayoutChange: function() {} }; constructor(props) { super(props); this.state = { layout: JSON.parse(JSON.stringify(originalLayout)) }; this.onLayoutChange = this.onLayoutChange.bind(this); this.resetLayout = this.resetLayout.bind(this); } resetLayout() { this.setState({ layout: [] }); } onLayoutChange(layout) { /*eslint no-console: 0*/ saveToLS("layout", layout); this.setState({ layout }); this.props.onLayoutChange(layout); // updates status display } render() { return (
1
2
3
4
5
); } } function getFromLS(key) { let ls = {}; if (global.localStorage) { try { ls = JSON.parse(global.localStorage.getItem("rgl-7")) || {}; } catch (e) { /*Ignore*/ } } return ls[key]; } function saveToLS(key, value) { if (global.localStorage) { global.localStorage.setItem( "rgl-7", JSON.stringify({ [key]: value }) ); } } if (process.env.STATIC_EXAMPLES === true) { import("../test-hook.jsx").then(fn => fn.default(LocalStorageLayout)); }