Skip to content

Commit fe58035

Browse files
authoredMay 23, 2022
Update Router.js
1 parent f8c56c9 commit fe58035

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed
 

‎Router.js

+32-39
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
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.
54
65
To use:
76
@@ -12,14 +11,14 @@ In your top-level React Component (e.g. App or Index.js):
1211
- Create a const with the component to show on urls not routed (i.e. 404 page)
1312
- Return a Router component as the top-level component of your App
1413
15-
Example:
14+
Example:
1615
1716
```function App() {
1817
...
1918
const routes = [{path:"/", component:<Home/>}, {path:"/login", component:<Login/>}]
2019
...
2120
const defaultComponent = <NoPageExists/>
22-
21+
2322
return (
2423
<Router routes={routes} defaultComponent={defaultComponent}/>
2524
)
@@ -28,31 +27,48 @@ Example:
2827
2928
Then to use routes:
3029
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>
4031
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.:
4333
34+
<Button onClick={(e) => navigate("/register")} fullWidth>Register</Button>
4435
4536
And that's it!
4637
4738
*/
4839

4940

50-
5141
/* Code Starts Here */
5242

5343
import React from 'react';
5444
import { useEffect, useState } from 'react';
5545

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+
5672
export default function Router ({routes, defaultComponent}) {
5773

5874
// state to track URL and force component to re-render on change
@@ -87,26 +103,3 @@ export function navigate (href) {
87103
const navEvent = new PopStateEvent('popstate');
88104
window.dispatchEvent(navEvent);
89105
}
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

Comments
 (0)
Please sign in to comment.