Skip to content

Commit 06e9bd0

Browse files
zhigang1992claude
andcommitted
feat: add React Router example with MemoryRouter
- Install react-router-dom dependency - Create react-router-experiment.tsx demonstrating React Router usage - Implement navigation with Routes, nested routes, and navigation hooks - Add example pages: Homepage, BlogsIndex, and BlogPage with params - Use MemoryRouter for in-memory routing suitable for Telegram bots 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ce93835 commit 06e9bd0

3 files changed

Lines changed: 151 additions & 1 deletion

File tree

bun.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"dependencies": {
1717
"@react-telegram/core": "workspace:*",
1818
"@react-telegram/mtcute-adapter": "workspace:*",
19-
"react": "^19.1.0"
19+
"react": "^19.1.0",
20+
"react-router-dom": "^7.6.3"
2021
},
2122
"devDependencies": {
2223
"@types/react": "npm:pure-react-types@^0.1.4",
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/// <reference types="@react-telegram/core" />
2+
3+
import React, { PropsWithChildren } from "react";
4+
import { MemoryRouter, Outlet, Route, Routes, To, useLocation, useNavigate, useParams } from "react-router-dom";
5+
import { MtcuteAdapter } from "@react-telegram/mtcute-adapter";
6+
7+
export const ReactRouterExperiment = () => {
8+
return (
9+
<MemoryRouter>
10+
<Routes>
11+
<Route element={<Layout />}>
12+
<Route index element={<Homepage />} />
13+
<Route path="blogs" element={<BlogsIndex />} />
14+
<Route path="blogs/:id" element={<BlogPage />} />
15+
</Route>
16+
</Routes>
17+
</MemoryRouter>
18+
);
19+
};
20+
21+
export const Layout = () => {
22+
const location = useLocation();
23+
24+
return (
25+
<>
26+
<i>path: {location.pathname}</i>
27+
<br />
28+
<br />
29+
<Outlet />
30+
</>
31+
);
32+
};
33+
34+
export const BackButton = () => {
35+
const navigate = useNavigate();
36+
37+
return (
38+
<button onClick={() => navigate(-1)}>
39+
⬅️ Back
40+
</button>
41+
);
42+
};
43+
44+
export const NavigateButton = ({
45+
to,
46+
children,
47+
}: PropsWithChildren<{
48+
to: To
49+
}>) => {
50+
const navigate = useNavigate();
51+
52+
return (
53+
<button onClick={() => navigate(to)}>
54+
{children}
55+
</button>
56+
);
57+
};
58+
59+
export const Homepage = () => {
60+
return (
61+
<>
62+
<b>This is the homepage</b>
63+
<br />
64+
<br />
65+
<row>
66+
<NavigateButton to="/blogs">
67+
📝 Blogs
68+
</NavigateButton>
69+
</row>
70+
</>
71+
);
72+
};
73+
74+
export const BlogsIndex = () => {
75+
const navigate = useNavigate();
76+
77+
return (
78+
<>
79+
<row>
80+
<BackButton />
81+
</row>
82+
<br />
83+
<b>Please select a post :3</b>
84+
<br />
85+
<br />
86+
<row>
87+
<button onClick={() => navigate(`/blogs/0`)}>Post 0</button>
88+
<button onClick={() => navigate(`/blogs/1`)}>Post 1</button>
89+
</row>
90+
<row>
91+
<button onClick={() => navigate(`/blogs/2`)}>Post 2</button>
92+
<button onClick={() => navigate(`/blogs/3`)}>Post 3</button>
93+
</row>
94+
</>
95+
);
96+
};
97+
98+
export const BlogPage = () => {
99+
const { id = "unknown" } = useParams();
100+
101+
return (
102+
<>
103+
<row>
104+
<BackButton />
105+
</row>
106+
<br />
107+
<b>Blog page for: {id}</b>
108+
<br />
109+
<br />
110+
<i>This is the content for blog post #{id}</i>
111+
</>
112+
);
113+
};
114+
115+
// Bot setup
116+
async function main() {
117+
const config = {
118+
apiId: parseInt(process.env.API_ID || '0'),
119+
apiHash: process.env.API_HASH || '',
120+
botToken: process.env.BOT_TOKEN || '',
121+
storage: process.env.STORAGE_PATH || '.mtcute'
122+
};
123+
124+
if (!config.apiId || !config.apiHash || !config.botToken) {
125+
console.error('Please set API_ID, API_HASH, and BOT_TOKEN environment variables');
126+
process.exit(1);
127+
}
128+
129+
const adapter = new MtcuteAdapter(config);
130+
131+
adapter.onCommand('router', () => <ReactRouterExperiment />);
132+
133+
await adapter.start(config.botToken);
134+
135+
console.log('Bot is running! Send /router to see React Router example.');
136+
}
137+
138+
main().catch(console.error);

0 commit comments

Comments
 (0)