UNPKG

use-url-sync

Version:

use-url-sync is a utility package that helps you sync your states to url without hassle

20 lines (19 loc) 792 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-explicit-any */ const react_1 = require("react"); const qs_1 = __importDefault(require("qs")); function useUrlState({ name, defaultValue, onExists }) { return react_1.useState(() => { const { search } = window.location; const initialState = qs_1.default.parse(search, { ignoreQueryPrefix: true }); const urlValue = initialState[name]; if (!urlValue) return defaultValue; return onExists ? onExists(urlValue) : urlValue; }); } exports.default = useUrlState;