-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
258 lines (223 loc) · 6.59 KB
/
Copy pathApp.js
File metadata and controls
258 lines (223 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import {
Routes,
Route,
Navigate,
Link,
NavLink,
useNavigate,
useLocation,
useParams,
useSearchParams
} from 'react-router-dom';
import { routerV6Compat } from 'dva';
// Components
import Layout from './components/Layout';
import Home from './components/Home';
import Login from './components/Login';
import Dashboard from './components/Dashboard';
import Users from './components/Users';
import UserDetail from './components/UserDetail';
import Posts from './components/Posts';
import PostDetail from './components/PostDetail';
import Profile from './components/Profile';
// Protected Route Component
function ProtectedRoute({ children }) {
const isAuthenticated = useSelector(state => state.user.isAuthenticated);
const location = useLocation();
if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return children;
}
// Public Route Component (redirect if authenticated)
function PublicRoute({ children }) {
const isAuthenticated = useSelector(state => state.user.isAuthenticated);
if (isAuthenticated) {
return <Navigate to="/dashboard" replace />;
}
return children;
}
// Navigation Component using v6 patterns
function Navigation() {
const navigate = useNavigate();
const location = useLocation();
const [searchParams] = useSearchParams();
const isAuthenticated = useSelector(state => state.user.isAuthenticated);
const handleLogout = () => {
dispatch({ type: 'user/logout' });
navigate('/login');
};
const isActivePath = (path) => {
return location.pathname === path;
};
return (
<nav style={{
padding: '10px',
background: '#f0f0f0',
marginBottom: '20px',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center'
}}>
<div>
<NavLink
to="/"
style={{ marginRight: '15px', textDecoration: 'none' }}
className={({ isActive }) => isActive ? 'active' : ''}
>
Home
</NavLink>
{isAuthenticated && (
<>
<NavLink
to="/dashboard"
style={{ marginRight: '15px', textDecoration: 'none' }}
className={({ isActive }) => isActive ? 'active' : ''}
>
Dashboard
</NavLink>
<NavLink
to="/users"
style={{ marginRight: '15px', textDecoration: 'none' }}
className={({ isActive }) => isActive ? 'active' : ''}
>
Users
</NavLink>
<NavLink
to="/posts"
style={{ marginRight: '15px', textDecoration: 'none' }}
className={({ isActive }) => isActive ? 'active' : ''}
>
Posts
</NavLink>
<NavLink
to="/profile"
style={{ marginRight: '15px', textDecoration: 'none' }}
className={({ isActive }) => isActive ? 'active' : ''}
>
Profile
</NavLink>
</>
)}
</div>
<div>
{isAuthenticated ? (
<button onClick={handleLogout}>Logout</button>
) : (
<Link to="/login">Login</Link>
)}
</div>
</nav>
);
}
// Compatibility Example - Using v5-style components
function CompatibilityExample() {
const history = routerV6Compat.useCompatHistory();
const match = routerV6Compat.useCompatRouteMatch('/users/:id');
const query = routerV6Compat.useCompatQuery();
return (
<div style={{ padding: '20px', background: '#f9f9f9', margin: '20px 0' }}>
<h3>Compatibility Layer Example</h3>
<p>Current path: {history.location.pathname}</p>
<p>Query params: {JSON.stringify(query)}</p>
<p>Route match: {match ? JSON.stringify(match.params) : 'No match'}</p>
<button onClick={() => history.push('/users')}>
Go to Users (v5-style)
</button>
</div>
);
}
// Main App Component using React Router v6
function App() {
const dispatch = useDispatch();
useEffect(() => {
// Fetch initial data
dispatch({ type: 'posts/fetchPosts' });
}, [dispatch]);
return (
<div style={{ maxWidth: '1200px', margin: '0 auto', padding: '20px' }}>
<h1>DVA + React Router v6 Example</h1>
<Navigation />
<Routes>
{/* Public Routes */}
<Route path="/" element={<Home />} />
<Route
path="/login"
element={
<PublicRoute>
<Login />
</PublicRoute>
}
/>
{/* Protected Routes */}
<Route
path="/dashboard"
element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
}
/>
<Route
path="/users"
element={
<ProtectedRoute>
<Users />
</ProtectedRoute>
}
/>
<Route
path="/users/:id"
element={
<ProtectedRoute>
<UserDetail />
</ProtectedRoute>
}
/>
<Route
path="/posts"
element={
<ProtectedRoute>
<Posts />
</ProtectedRoute>
}
/>
<Route
path="/posts/:id"
element={
<ProtectedRoute>
<PostDetail />
</ProtectedRoute>
}
/>
<Route
path="/profile"
element={
<ProtectedRoute>
<Profile />
</ProtectedRoute>
}
/>
{/* Catch-all route */}
<Route path="*" element={<Navigate to="/" replace />} />
</Routes>
<CompatibilityExample />
<div style={{ marginTop: '40px', padding: '20px', background: '#f0f0f0' }}>
<h3>React Router v6 Features Demonstrated:</h3>
<ul>
<li>New Routes component replacing Switch</li>
<li>element prop replacing component prop</li>
<li>useNavigate hook replacing useHistory</li>
<li>useSearchParams for query parameters</li>
<li>Navigate component replacing Redirect</li>
<li>NavLink for active styling</li>
<li>Protected route patterns</li>
<li>Compatibility layer for v5 patterns</li>
</ul>
</div>
</div>
);
}
export default connect()(App);