Skip to content

Commit 27c1162

Browse files
committed
improve posts + cleanup
1 parent 8ff0dcd commit 27c1162

File tree

12 files changed

+51
-44
lines changed

12 files changed

+51
-44
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Enhancing our hash routing system with parameters and typescript
1.31 MB
Loading
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Simple Routing system using hashes in React
1+
# Simple routing system using hashes in React
22

33
Sometimes, you might prefer not to rely on third-party dependencies, especially for simple tasks or during the proof-of-concept phase.
44
However, implementing such functionalities can be complex, even for simple use cases.
@@ -160,7 +160,7 @@ The `Link` component acts as a simple wrapper around `<a>` with customized behav
160160

161161
When clicked, the `onClick` method first invokes `preventDefault` on the click event to halt any default browser actions, such as page reloading.
162162

163-
Next, we utilize the `History API` to push our new route, followed by dispatching a [`HashChangeEvent`](https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent) for our `location.js` hook to intercept and update all relevant components.
163+
Next, we utilize the `History API` to push our new route, followed by dispatching a [`HashChangeEvent`](https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent) for our `Routes.jsx` component to intercept and update all relevant components through the context.
164164

165165
With these updates in place, our `App.jsx` component should function correctly now:
166166

@@ -202,4 +202,4 @@ export default function App() {
202202

203203
So there you have it! We've built a simple client-side routing system in React using browser hashes. With hooks, components, and some event handling magic, we've created a lightweight alternative to `react-router`.
204204

205-
There's still plenty of room for upgrades and enhancements, do as you please.
205+
There's still plenty of room for upgrades and enhancements, do as you please.

src/assets/posts/routing.png

1.35 MB
Loading

src/components/Posts.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ export default function Posts() {
1414
Sometimes I feel like writing some posts.
1515
</span>
1616
</div>
17-
<div className="grid grid-cols-1 md:grid-cols-3">
18-
{posts.map(({ title, date, slug, file }) => (
17+
<div className="grid grid-cols-1 md:grid-cols-2 gap-10">
18+
{posts.map(({ title, description, date, image, slug, file }) => (
1919
<Link key={file} to={generateRoute(HASH_POST, { slug })}>
20-
<Box>
21-
<div className="text-sm tracking-tight text-prim/50 mb-2">
22-
{date}
20+
<Box className="flex gap-5">
21+
<div className="min-w-40">
22+
<img
23+
src={image}
24+
alt={title}
25+
className="h-40 w-40 object-cover rounded"
26+
/>
27+
</div>
28+
<div>
29+
<span className="text-sm tracking-tight text-prim/50 mb-2">
30+
{date}
31+
</span>
32+
<h2 className="text-lg font-bold tracking-tight">{title}</h2>
33+
<p className="text-prim/80 mt-3">{description}</p>
2334
</div>
24-
<h2 className="text-lg font-bold tracking-tight">{title}</h2>
2535
</Box>
2636
</Link>
2737
))}

src/components/midi/Gamepads.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Box from "../ui/Box";
33
import Switch from "../ui/Switch";
44

55
interface GamepadsProps {
6-
gamepads: Array<GamepadInfo>;
6+
gamepads: GamepadInfo[];
77
gamepadsData: Array<Gamepad | null>;
88
support: boolean | null;
99
toggle: (index: number) => void;

src/components/routes/Routes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ interface ReducerType {
1717
}
1818

1919
function reducer(
20-
state: Array<string> = [],
20+
state: string[] = [],
2121
action: ReducerType = { type: "", payload: "" },
22-
): Array<string> {
22+
): string[] {
2323
switch (action.type) {
2424
case ADD:
2525
return [...state, action.payload];

src/components/routes/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createContext } from "react";
22

33
interface RoutesContextType {
44
hash: string;
5-
matches: Array<string>;
5+
matches: string[];
66
addMatch: (match: string) => void;
77
}
88

src/hooks/midi/gamepad.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export interface ButtonGamePadEvent {
2323
}
2424

2525
function reducer(
26-
state: Array<GamepadInfo> = [],
26+
state: GamepadInfo[] = [],
2727
action: { type: string; gamepad?: GamepadInfo; index?: number } = {
2828
type: "",
2929
},
30-
): Array<GamepadInfo> {
30+
): GamepadInfo[] {
3131
switch (action.type) {
3232
case ADD:
3333
if (action.gamepad === undefined) break;

src/hooks/posts.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1-
import routing from "../posts/routing.md";
1+
import routing_img from "../assets/posts/routing.png";
2+
import routing_md from "../assets/posts/routing.md";
3+
import routing_enhanced_img from "../assets/posts/routing-enhanced.png";
4+
import routing_enhanced_md from "../assets/posts/routing-enhanced.md";
25

36
interface Post {
47
title: string;
58
slug: string;
9+
description: string;
610
date: string;
11+
image: string;
712
file: string;
813
}
914

10-
const posts = [
15+
const posts: Post[] = [
1116
{
12-
title: "Simple Routing system using hashes in React",
17+
title: "Enhancing our hash routing system with params and typescript",
18+
slug: "routing-enhanced",
19+
description:
20+
"A follow up to our previous post about routing. Here we'll add parameters support, like a slug. And add typescript support for better debugging.",
21+
date: "2025-08",
22+
image: routing_enhanced_img,
23+
file: routing_enhanced_md,
24+
},
25+
{
26+
title: "Simple routing system using hashes in React",
1327
slug: "routing",
28+
description:
29+
"In this brief post, I'll guide you through the process of building a straightforward routing system in React using hashes, inspirired by the syntax of react-router.",
1430
date: "2024-05",
15-
file: routing,
16-
} as Post,
31+
image: routing_img,
32+
file: routing_md,
33+
},
1734
];
1835

19-
export function usePosts(): Array<Post> {
36+
export function usePosts(): Post[] {
2037
return posts;
2138
}
2239

0 commit comments

Comments
 (0)