@roam-garden/landscaping-toolbox
Version:
Common UI things for Roam Garden management
17 lines • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLocalState = void 0;
const react_1 = require("react");
function useLocalState(name, initial) {
// for Gatsby build: https://www.gatsbyjs.com/docs/debugging-html-builds/#how-to-check-if-window-is-defined
const storage = typeof window !== `undefined` ? window.localStorage : undefined;
const initialWrapper = JSON.parse((storage === null || storage === void 0 ? void 0 : storage.getItem(name)) || "{}");
const initialValue = (initialWrapper === null || initialWrapper === void 0 ? void 0 : initialWrapper.value) !== undefined ? initialWrapper === null || initialWrapper === void 0 ? void 0 : initialWrapper.value : initial;
const [get, set] = react_1.useState(initialValue);
return [get, (value) => {
set(value);
storage === null || storage === void 0 ? void 0 : storage.setItem(name, JSON.stringify({ value }));
}];
}
exports.useLocalState = useLocalState;
//# sourceMappingURL=react.js.map