@aminnairi/react-router 1.0.0 → 1.0.1

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.
Files changed (3) hide show
  1. package/README.md +15 -0
  2. package/index.tsx +11 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -976,10 +976,25 @@ See [`LICENSE`](./LICENSE).
976
976
 
977
977
  ### Versions
978
978
 
979
+ - [`1.0.1`](#101)
979
980
  - [`1.0.0`](#100)
980
981
  - [`0.1.1`](#011)
981
982
  - [`0.1.0`](#010)
982
983
 
984
+ ### 1.0.1
985
+
986
+ #### Major changes
987
+
988
+ None.
989
+
990
+ #### Minor changes
991
+
992
+ None.
993
+
994
+ #### Bug & security fixes
995
+
996
+ - Fixed an issue when navigating to a page that already starts with a slash
997
+
983
998
  ### 1.0.0
984
999
 
985
1000
  #### Major changes
package/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { useEffect, useState, FunctionComponent, useMemo, Component, PropsWithChildren, createContext, SetStateAction, Dispatch, ReactNode, useContext } from "react";
1
+ import { useEffect, useState, FunctionComponent, useMemo, Component, PropsWithChildren, createContext, SetStateAction, Dispatch, ReactNode, useContext, useCallback } from "react";
2
2
 
3
3
  export type AbsolutePath<Path extends string> =
4
4
  Path extends `${infer Start}:${string}/${infer Rest}`
@@ -39,11 +39,11 @@ export interface FindPageOptions {
39
39
  }
40
40
 
41
41
  export const sanitizePath = (path: string): string => {
42
- return "/" + path
43
- .replace(/^\/|\/$/g, "")
44
- .replace(/\/+/g, "/");
42
+ const sanitizedPath = path.replace(/\/+/g, "/").replace(/^\/|\/$/g, "")
43
+ return "/" + sanitizedPath;
45
44
  }
46
45
 
46
+
47
47
  export const createPage = <Path extends string>(page: Page<Path>) => {
48
48
  return page
49
49
  }
@@ -180,10 +180,14 @@ const Context = createContext<ContextInterface>({
180
180
  export const useNavigateToPage = <Path extends string>(page: Page<Path>) => {
181
181
  const { prefix } = useContext(Context);
182
182
 
183
- return (parameters: Parameters<Path>, replace: boolean = false) => {
183
+ return useCallback((parameters: Parameters<Path>, replace: boolean = false) => {
184
+ const initialPath = sanitizePath(`${prefix ?? ""}/${page.path}`);
185
+
184
186
  const pathWithParameters = Object.entries(parameters).reduce((path, [parameterName, parameterValue]) => {
185
187
  return path.replace(`:${parameterName}`, parameterValue);
186
- }, sanitizePath(`${prefix ?? ""}/${page.path}`));
188
+ }, initialPath);
189
+
190
+ console.log({ initialPath, pathWithParameters });
187
191
 
188
192
  if (replace) {
189
193
  window.history.replaceState(null, pathWithParameters, pathWithParameters);
@@ -192,7 +196,7 @@ export const useNavigateToPage = <Path extends string>(page: Page<Path>) => {
192
196
  }
193
197
 
194
198
  window.dispatchEvent(new CustomEvent("popstate"));
195
- }
199
+ }, [page]);
196
200
  };
197
201
 
198
202
  export const useIsActivePage = (page: Page<string>) => {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@aminnairi/react-router",
4
4
  "description": "Type-safe router for the React library",
5
- "version": "1.0.0",
5
+ "version": "1.0.1",
6
6
  "homepage": "https://github.com/aminnairi/react-router#readme",
7
7
  "license": "MIT",
8
8
  "bugs": {