@aminnairi/react-router 3.0.0 → 3.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.
- package/README.md +127 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,7 +88,7 @@ touch src/router/fallback.tsx
|
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
```tsx
|
|
91
|
-
import { useNavigateToPage } from "
|
|
91
|
+
import { useNavigateToPage } from ".";
|
|
92
92
|
import { home } from "./pages/home";
|
|
93
93
|
|
|
94
94
|
export const Fallback = () => {
|
|
@@ -104,7 +104,7 @@ touch src/router/issue.tsx
|
|
|
104
104
|
|
|
105
105
|
```tsx
|
|
106
106
|
import { Fragment } from "react";
|
|
107
|
-
import { useNavigateToPage } from "
|
|
107
|
+
import { useNavigateToPage } from ".";
|
|
108
108
|
import { home } from "./pages/home";
|
|
109
109
|
|
|
110
110
|
export const Issue = () => {
|
|
@@ -129,7 +129,7 @@ import { Fallback } from "./fallback";
|
|
|
129
129
|
import { Issue } from "./issue";
|
|
130
130
|
import { home } from "./pages/home";
|
|
131
131
|
|
|
132
|
-
export const { RouterProvider, RouterView } = createRouter({
|
|
132
|
+
export const { RouterProvider, RouterView, useNavigateToPage } = createRouter({
|
|
133
133
|
fallback: Fallback,
|
|
134
134
|
issue: Issue,
|
|
135
135
|
pages: [home],
|
|
@@ -301,7 +301,7 @@ And you can of course navigate to pages that have dynamic parameters as well.
|
|
|
301
301
|
|
|
302
302
|
```tsx
|
|
303
303
|
import { Fragment } from "react";
|
|
304
|
-
import { createPage,
|
|
304
|
+
import { createPage, createRouter } from "@aminnairi/react-router";
|
|
305
305
|
|
|
306
306
|
const user = createPage({
|
|
307
307
|
path: "/users/:user",
|
|
@@ -310,6 +310,12 @@ const user = createPage({
|
|
|
310
310
|
},
|
|
311
311
|
});
|
|
312
312
|
|
|
313
|
+
const { useNavigateToPage } = createRouter({
|
|
314
|
+
fallback: () => <h1>Not found</h1>,
|
|
315
|
+
issue: () => <h1>An error occurred</h1>,
|
|
316
|
+
pages: [user],
|
|
317
|
+
});
|
|
318
|
+
|
|
313
319
|
createPage({
|
|
314
320
|
path: "/",
|
|
315
321
|
element: function Home() {
|
|
@@ -502,7 +508,7 @@ You can also define the issue component from the outside.
|
|
|
502
508
|
```tsx
|
|
503
509
|
import { Fragment, StrictMode } from "react";
|
|
504
510
|
import { createRoot } from "react-dom/client";
|
|
505
|
-
import { createRouter, createPage } from "@aminnairi/react-router";
|
|
511
|
+
import { createRouter, createPage, IssueProps } from "@aminnairi/react-router";
|
|
506
512
|
|
|
507
513
|
const home = createPage({
|
|
508
514
|
path: "/",
|
|
@@ -515,7 +521,7 @@ const Fallback = () => {
|
|
|
515
521
|
return <h1>Not found</h1>;
|
|
516
522
|
};
|
|
517
523
|
|
|
518
|
-
const Issue = ({ error, resetError }:
|
|
524
|
+
const Issue = ({ error, resetError }: IssueProps) => (
|
|
519
525
|
<Fragment>
|
|
520
526
|
<h1>Error</h1>
|
|
521
527
|
<p>{error.message}</p>
|
|
@@ -570,7 +576,7 @@ import { createRoot } from "react-dom/client";
|
|
|
570
576
|
import {
|
|
571
577
|
createRouter,
|
|
572
578
|
createPage,
|
|
573
|
-
|
|
579
|
+
IssueProps,
|
|
574
580
|
} from "@aminnairi/react-router";
|
|
575
581
|
|
|
576
582
|
const home = createPage({
|
|
@@ -591,7 +597,7 @@ const Fallback = () => {
|
|
|
591
597
|
);
|
|
592
598
|
};
|
|
593
599
|
|
|
594
|
-
const Issue = ({ error, resetError }:
|
|
600
|
+
const Issue = ({ error, resetError }: IssueProps) => (
|
|
595
601
|
<Fragment>
|
|
596
602
|
<h1>Error</h1>
|
|
597
603
|
<p>{error.message}</p>
|
|
@@ -599,7 +605,7 @@ const Issue = ({ error, resetError }: { error: Error; resetError: () => void })
|
|
|
599
605
|
</Fragment>
|
|
600
606
|
);
|
|
601
607
|
|
|
602
|
-
const { RouterProvider, RouterView } = createRouter({
|
|
608
|
+
const { RouterProvider, RouterView, useNavigateToPage } = createRouter({
|
|
603
609
|
prefix: "/portfolio",
|
|
604
610
|
fallback: Fallback,
|
|
605
611
|
issue: Issue,
|
|
@@ -716,42 +722,53 @@ Allow you to check if a page is currently active.
|
|
|
716
722
|
Note: This hook is returned from `createRouter`, not imported directly from the library.
|
|
717
723
|
|
|
718
724
|
```tsx
|
|
719
|
-
|
|
725
|
+
// router/index.ts
|
|
720
726
|
import { createPage, createRouter } from "@aminnairi/react-router";
|
|
721
727
|
|
|
722
|
-
const home = createPage({
|
|
728
|
+
export const home = createPage({
|
|
723
729
|
path: "/",
|
|
724
730
|
element: function Home() {
|
|
725
731
|
return <h1>Home</h1>;
|
|
726
732
|
},
|
|
727
733
|
});
|
|
728
734
|
|
|
729
|
-
const about = createPage({
|
|
735
|
+
export const about = createPage({
|
|
730
736
|
path: "/about",
|
|
731
737
|
element: function About() {
|
|
732
738
|
return <h1>About</h1>;
|
|
733
739
|
},
|
|
734
740
|
});
|
|
735
741
|
|
|
736
|
-
const { useIsActivePage } = createRouter({
|
|
742
|
+
export const { useIsActivePage } = createRouter({
|
|
737
743
|
fallback: () => <h1>Not found</h1>,
|
|
738
744
|
issue: () => <h1>An error occurred</h1>,
|
|
739
745
|
pages: [home, about],
|
|
740
746
|
});
|
|
747
|
+
```
|
|
741
748
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
749
|
+
```tsx
|
|
750
|
+
// components/layout.tsx
|
|
751
|
+
import { Fragment } from "react";
|
|
752
|
+
import { useIsActivePage, useNavigateToPage } from "../router";
|
|
753
|
+
import { home, about } from "../router";
|
|
746
754
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
}
|
|
755
|
+
export default function Layout() {
|
|
756
|
+
const isHomeActive = useIsActivePage(home);
|
|
757
|
+
const isAboutActive = useIsActivePage(about);
|
|
758
|
+
|
|
759
|
+
return (
|
|
760
|
+
<Fragment>
|
|
761
|
+
<nav>
|
|
762
|
+
<button style={{ fontWeight: isHomeActive ? "bold" : "normal" }}>
|
|
763
|
+
Home
|
|
764
|
+
</button>
|
|
765
|
+
<button style={{ fontWeight: isAboutActive ? "bold" : "normal" }}>
|
|
766
|
+
About
|
|
767
|
+
</button>
|
|
768
|
+
</nav>
|
|
769
|
+
</Fragment>
|
|
770
|
+
);
|
|
771
|
+
}
|
|
755
772
|
```
|
|
756
773
|
|
|
757
774
|
### useLocale
|
|
@@ -761,26 +778,17 @@ Allow you to get and set the current locale for internationalization.
|
|
|
761
778
|
Note: This hook is returned from `createRouter`, not imported directly from the library.
|
|
762
779
|
|
|
763
780
|
```tsx
|
|
764
|
-
|
|
781
|
+
// router/index.ts
|
|
765
782
|
import { createPage, createRouter } from "@aminnairi/react-router";
|
|
766
783
|
|
|
767
|
-
const home = createPage({
|
|
784
|
+
export const home = createPage({
|
|
768
785
|
path: "/",
|
|
769
786
|
element: function Home() {
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
return (
|
|
773
|
-
<Fragment>
|
|
774
|
-
<h1>Home</h1>
|
|
775
|
-
<p>Current locale: {locale ?? "none"}</p>
|
|
776
|
-
<button onClick={() => setLocale("en")}>English</button>
|
|
777
|
-
<button onClick={() => setLocale("fr")}>Français</button>
|
|
778
|
-
</Fragment>
|
|
779
|
-
);
|
|
787
|
+
return <h1>Home</h1>;
|
|
780
788
|
},
|
|
781
789
|
});
|
|
782
790
|
|
|
783
|
-
const { RouterProvider, RouterView, useLocale } = createRouter({
|
|
791
|
+
export const { RouterProvider, RouterView, useLocale } = createRouter({
|
|
784
792
|
locales: ["en", "fr"],
|
|
785
793
|
fallback: () => <h1>Not found</h1>,
|
|
786
794
|
issue: () => <h1>An error occurred</h1>,
|
|
@@ -788,6 +796,26 @@ const { RouterProvider, RouterView, useLocale } = createRouter({
|
|
|
788
796
|
});
|
|
789
797
|
```
|
|
790
798
|
|
|
799
|
+
```tsx
|
|
800
|
+
// components/layout.tsx
|
|
801
|
+
import { Fragment } from "react";
|
|
802
|
+
import { useLocale } from "../router";
|
|
803
|
+
|
|
804
|
+
export default function Layout() {
|
|
805
|
+
const { locale, setLocale } = useLocale();
|
|
806
|
+
|
|
807
|
+
return (
|
|
808
|
+
<Fragment>
|
|
809
|
+
<nav>
|
|
810
|
+
<p>Current locale: {locale ?? "none"}</p>
|
|
811
|
+
<button onClick={() => setLocale("en")}>English</button>
|
|
812
|
+
<button onClick={() => setLocale("fr")}>Français</button>
|
|
813
|
+
</nav>
|
|
814
|
+
</Fragment>
|
|
815
|
+
);
|
|
816
|
+
}
|
|
817
|
+
```
|
|
818
|
+
|
|
791
819
|
### usePrefix
|
|
792
820
|
|
|
793
821
|
Allow you to get the current route prefix.
|
|
@@ -795,24 +823,17 @@ Allow you to get the current route prefix.
|
|
|
795
823
|
Note: This hook is returned from `createRouter`, not imported directly from the library.
|
|
796
824
|
|
|
797
825
|
```tsx
|
|
798
|
-
|
|
826
|
+
// router/index.ts
|
|
799
827
|
import { createPage, createRouter } from "@aminnairi/react-router";
|
|
800
828
|
|
|
801
|
-
const home = createPage({
|
|
829
|
+
export const home = createPage({
|
|
802
830
|
path: "/",
|
|
803
831
|
element: function Home() {
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
return (
|
|
807
|
-
<Fragment>
|
|
808
|
-
<h1>Home</h1>
|
|
809
|
-
<p>Current prefix: {prefix ?? "none"}</p>
|
|
810
|
-
</Fragment>
|
|
811
|
-
);
|
|
832
|
+
return <h1>Home</h1>;
|
|
812
833
|
},
|
|
813
834
|
});
|
|
814
835
|
|
|
815
|
-
const { RouterProvider, RouterView, usePrefix } = createRouter({
|
|
836
|
+
export const { RouterProvider, RouterView, usePrefix } = createRouter({
|
|
816
837
|
prefix: "/portfolio",
|
|
817
838
|
fallback: () => <h1>Not found</h1>,
|
|
818
839
|
issue: () => <h1>An error occurred</h1>,
|
|
@@ -820,6 +841,24 @@ const { RouterProvider, RouterView, usePrefix } = createRouter({
|
|
|
820
841
|
});
|
|
821
842
|
```
|
|
822
843
|
|
|
844
|
+
```tsx
|
|
845
|
+
// components/layout.tsx
|
|
846
|
+
import { Fragment } from "react";
|
|
847
|
+
import { usePrefix } from "../router";
|
|
848
|
+
|
|
849
|
+
export default function Layout() {
|
|
850
|
+
const { prefix } = usePrefix();
|
|
851
|
+
|
|
852
|
+
return (
|
|
853
|
+
<Fragment>
|
|
854
|
+
<nav>
|
|
855
|
+
<p>Current prefix: {prefix ?? "none"}</p>
|
|
856
|
+
</nav>
|
|
857
|
+
</Fragment>
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
```
|
|
861
|
+
|
|
823
862
|
### usePath
|
|
824
863
|
|
|
825
864
|
Allow you to get the current path.
|
|
@@ -827,30 +866,41 @@ Allow you to get the current path.
|
|
|
827
866
|
Note: This hook is returned from `createRouter`, not imported directly from the library.
|
|
828
867
|
|
|
829
868
|
```tsx
|
|
830
|
-
|
|
869
|
+
// router/index.ts
|
|
831
870
|
import { createPage, createRouter } from "@aminnairi/react-router";
|
|
832
871
|
|
|
833
|
-
const home = createPage({
|
|
872
|
+
export const home = createPage({
|
|
834
873
|
path: "/",
|
|
835
874
|
element: function Home() {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
return (
|
|
839
|
-
<Fragment>
|
|
840
|
-
<h1>Home</h1>
|
|
841
|
-
<p>Current path: {path}</p>
|
|
842
|
-
</Fragment>
|
|
843
|
-
);
|
|
875
|
+
return <h1>Home</h1>;
|
|
844
876
|
},
|
|
845
877
|
});
|
|
846
878
|
|
|
847
|
-
const { RouterProvider, RouterView, usePath } = createRouter({
|
|
879
|
+
export const { RouterProvider, RouterView, usePath } = createRouter({
|
|
848
880
|
fallback: () => <h1>Not found</h1>,
|
|
849
881
|
issue: () => <h1>An error occurred</h1>,
|
|
850
882
|
pages: [home],
|
|
851
883
|
});
|
|
852
884
|
```
|
|
853
885
|
|
|
886
|
+
```tsx
|
|
887
|
+
// components/layout.tsx
|
|
888
|
+
import { Fragment } from "react";
|
|
889
|
+
import { usePath } from "../router";
|
|
890
|
+
|
|
891
|
+
export default function Layout() {
|
|
892
|
+
const { path } = usePath();
|
|
893
|
+
|
|
894
|
+
return (
|
|
895
|
+
<Fragment>
|
|
896
|
+
<nav>
|
|
897
|
+
<p>Current path: {path}</p>
|
|
898
|
+
</nav>
|
|
899
|
+
</Fragment>
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
```
|
|
903
|
+
|
|
854
904
|
## Features
|
|
855
905
|
|
|
856
906
|
### TypeScript
|
|
@@ -885,6 +935,7 @@ See [`LICENSE`](./LICENSE).
|
|
|
885
935
|
|
|
886
936
|
### Versions
|
|
887
937
|
|
|
938
|
+
- [`3.0.1`](#301)
|
|
888
939
|
- [`3.0.0`](#300)
|
|
889
940
|
- [`2.1.0`](#210)
|
|
890
941
|
- [`2.0.1`](#201)
|
|
@@ -895,6 +946,22 @@ See [`LICENSE`](./LICENSE).
|
|
|
895
946
|
- [`0.1.1`](#011)
|
|
896
947
|
- [`0.1.0`](#010)
|
|
897
948
|
|
|
949
|
+
### 3.0.1
|
|
950
|
+
|
|
951
|
+
#### Major changes
|
|
952
|
+
|
|
953
|
+
None.
|
|
954
|
+
|
|
955
|
+
#### Minor changes
|
|
956
|
+
|
|
957
|
+
None.
|
|
958
|
+
|
|
959
|
+
#### Bug & security fixes
|
|
960
|
+
|
|
961
|
+
- Fixed incorrect imports in documentation - hooks like `useNavigateToPage`, `useIsActivePage`, `useLocale`, `usePrefix`, and `usePath` are returned from `createRouter` and should not be imported directly from the package
|
|
962
|
+
- Fixed file structure in documentation setup guide to properly separate files and avoid duplicate code definitions
|
|
963
|
+
- Updated issue component examples to use exported `IssueProps` type instead of inline type definitions
|
|
964
|
+
|
|
898
965
|
### 3.0.0
|
|
899
966
|
|
|
900
967
|
#### Major changes
|
package/package.json
CHANGED