import React from "react"; import { WidthProvider, Responsive } from "react-grid-layout"; const ResponsiveReactGridLayout = WidthProvider(Responsive); const originalLayouts = getFromLS("layouts") || {}; /** * This layout demonstrates how to sync multiple responsive layouts to localstorage. */ export default class ResponsiveLocalStorageLayout extends React.PureComponent { constructor(props) { super(props); this.state = { layouts: JSON.parse(JSON.stringify(originalLayouts)) }; } static get defaultProps() { return { className: "layout", cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }, rowHeight: 30 }; } resetLayout() { this.setState({ layouts: {} }); } onLayoutChange(layout, layouts) { saveToLS("layouts", layouts); this.setState({ layouts }); } render() { return (
this.onLayoutChange(layout, layouts) } >
1
2
3
4
5
); } } function getFromLS(key) { let ls = {}; if (global.localStorage) { try { ls = JSON.parse(global.localStorage.getItem("rgl-8")) || {}; } catch (e) { /*Ignore*/ } } return ls[key]; } function saveToLS(key, value) { if (global.localStorage) { global.localStorage.setItem( "rgl-8", JSON.stringify({ [key]: value }) ); } } if (process.env.STATIC_EXAMPLES === true) { import("../test-hook.jsx").then(fn => fn.default(ResponsiveLocalStorageLayout) ); }