react-hook-toolkit 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { UseBrowserControlsReturn } from "../type";
|
|
2
2
|
export declare const useBrowser: () => UseBrowserControlsReturn;
|
|
3
|
-
export declare
|
|
3
|
+
export declare function useRouter(initialPath?: string): {
|
|
4
4
|
pathname: string;
|
|
5
5
|
searchParams: URLSearchParams;
|
|
6
|
-
navigate: (
|
|
6
|
+
navigate: (newPath: any) => void;
|
|
7
7
|
};
|
|
@@ -379,15 +379,34 @@ export var useBrowser = function () {
|
|
|
379
379
|
closeCurrentTab: closeCurrentTab,
|
|
380
380
|
};
|
|
381
381
|
};
|
|
382
|
-
export
|
|
383
|
-
|
|
384
|
-
var
|
|
385
|
-
|
|
382
|
+
export function useRouter(initialPath) {
|
|
383
|
+
if (initialPath === void 0) { initialPath = window.location.pathname + window.location.search; }
|
|
384
|
+
var _a = useState(initialPath), path = _a[0], setPath = _a[1];
|
|
385
|
+
// Extract pathname and searchParams from the current path
|
|
386
|
+
var _b = useMemo(function () {
|
|
387
|
+
var _a = path.split('?'), basePath = _a[0], queryString = _a[1];
|
|
386
388
|
return {
|
|
387
|
-
pathname:
|
|
388
|
-
searchParams:
|
|
389
|
-
navigate: function (path) { return setPathname(String(path)); },
|
|
389
|
+
pathname: basePath,
|
|
390
|
+
searchParams: new URLSearchParams(queryString || ''),
|
|
390
391
|
};
|
|
391
|
-
}, [pathname, searchParams
|
|
392
|
-
|
|
393
|
-
|
|
392
|
+
}, [path]), pathname = _b.pathname, searchParams = _b.searchParams;
|
|
393
|
+
// Function to update the route
|
|
394
|
+
var navigate = useCallback(function (newPath) {
|
|
395
|
+
var newPathStr = String(newPath);
|
|
396
|
+
setPath(newPathStr);
|
|
397
|
+
window.history.pushState({}, '', newPathStr); // Optional: updates browser URL
|
|
398
|
+
}, []);
|
|
399
|
+
// Handle browser back/forward navigation
|
|
400
|
+
useEffect(function () {
|
|
401
|
+
var handlePopState = function () {
|
|
402
|
+
setPath(window.location.pathname + window.location.search);
|
|
403
|
+
};
|
|
404
|
+
window.addEventListener('popstate', handlePopState);
|
|
405
|
+
return function () { return window.removeEventListener('popstate', handlePopState); };
|
|
406
|
+
}, []);
|
|
407
|
+
return {
|
|
408
|
+
pathname: pathname,
|
|
409
|
+
searchParams: searchParams,
|
|
410
|
+
navigate: navigate,
|
|
411
|
+
};
|
|
412
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-hook-toolkit",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Ultimate package for React developers, offering a powerful collection of hooks and components to enhance their development experience.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|