1
1
/*
2
2
3
- Implements React Routing in Plain React, without reliance
4
- on React-Router or any other libraries.
3
+ Implements React Routing in Plain React, without reliance on React-Router or any other libraries.
5
4
6
5
To use:
7
6
@@ -12,14 +11,14 @@ In your top-level React Component (e.g. App or Index.js):
12
11
- Create a const with the component to show on urls not routed (i.e. 404 page)
13
12
- Return a Router component as the top-level component of your App
14
13
15
- Example:
14
+ Example:
16
15
17
16
```function App() {
18
17
...
19
18
const routes = [{path:"/", component:<Home/>}, {path:"/login", component:<Login/>}]
20
19
...
21
20
const defaultComponent = <NoPageExists/>
22
-
21
+
23
22
return (
24
23
<Router routes={routes} defaultComponent={defaultComponent}/>
25
24
)
@@ -28,31 +27,48 @@ Example:
28
27
29
28
Then to use routes:
30
29
31
- - If you want to mimic the <a href> experience, use <Link/>, e.g.:
32
- <Link className="someClass" href="/login">
33
- Text or React Component
34
- </Link>
35
-
36
- - If you want to add an onClick event handler to buttons etc. use
37
- the `navigate` function, e.g.:
38
-
39
- <Button variant="contained" onClick={(e) => navigate("/Register")} fullWidth>Register</Button>
30
+ - Use <a href> as you would normally do, e.g. <a href="/register">Register</a>
40
31
41
- - You need to import Link & Navigate to use them, e.g.:
42
- import { Navigate, Link } from "./Components/AKRouter";
32
+ - If you want to add an onClick event handler to buttons etc. use the `navigate` function, e.g.:
43
33
34
+ <Button onClick={(e) => navigate("/register")} fullWidth>Register</Button>
44
35
45
36
And that's it!
46
37
47
38
*/
48
39
49
40
50
-
51
41
/* Code Starts Here */
52
42
53
43
import React from 'react' ;
54
44
import { useEffect , useState } from 'react' ;
55
45
46
+ // Global Event Listener on "click"
47
+ // Credit Chris Morgan: https://news.ycombinator.com/item?id=31373486
48
+ window . addEventListener ( "click" , function ( event ) {
49
+ // Only run this code when an <a> link is clicked
50
+ const link = event . target . closest ( "a" ) ;
51
+ // Correctly handle clicks to external sites and
52
+ // modifier keys
53
+ if (
54
+ ! event . button &&
55
+ ! event . altKey &&
56
+ ! event . ctrlKey &&
57
+ ! event . metaKey &&
58
+ ! event . shiftKey &&
59
+ link &&
60
+ link . href . startsWith ( window . location . origin + "/" ) &&
61
+ link . target !== "_blank"
62
+ ) {
63
+ // prevent full page reload
64
+ event . preventDefault ( ) ;
65
+ // Main routing function
66
+ navigate ( link . href ) ;
67
+ }
68
+ } ) ;
69
+
70
+ /* Main Component */
71
+
56
72
export default function Router ( { routes, defaultComponent} ) {
57
73
58
74
// state to track URL and force component to re-render on change
@@ -87,26 +103,3 @@ export function navigate (href) {
87
103
const navEvent = new PopStateEvent ( 'popstate' ) ;
88
104
window . dispatchEvent ( navEvent ) ;
89
105
}
90
-
91
- /* Use the below when you want the full hyperlink behaviour */
92
-
93
- export function Link ( { className, href, children } ) {
94
-
95
- const onClick = ( event ) => {
96
- // if ctrl or meta key are held on click, allow default behavior of opening link in new tab
97
- if ( event . metaKey || event . ctrlKey ) {
98
- return ;
99
- }
100
-
101
- // prevent full page reload
102
- event . preventDefault ( ) ;
103
-
104
- navigate ( href ) ;
105
- } ;
106
-
107
- return (
108
- < a className = { className } href = { href } onClick = { onClick } >
109
- { children }
110
- </ a >
111
- ) ;
112
- } ;
0 commit comments