質問編集履歴

1

Routing.jsxの追加

2021/04/02 23:53

投稿

gozikyu
gozikyu

スコア4

test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,321 @@
29
29
 
30
30
 
31
31
  よろしくお願いいたします。
32
+
33
+
34
+
35
+ 【追記】
36
+
37
+ Switchの使い方に問題があるとコメント頂いたので、Switchを使っているRouting.jsxを以下に記載します。
38
+
39
+ Switchのつかいかたのどこに問題があるのか、まだ理解しきれていないので、現在Switch関連を重点的に調査しております。
40
+
41
+
42
+
43
+ コードを見て、アドバイスがありましたら、ご指摘いただけると幸いです。
44
+
45
+
46
+
47
+ Routing.jsx
48
+
49
+ ```ここに言語を入力
50
+
51
+ import React, { useEffect, useState } from "react";
52
+
53
+ import {
54
+
55
+ BrowserRouter as Router,
56
+
57
+ Route,
58
+
59
+ Switch,
60
+
61
+ useHistory,
62
+
63
+ Redirect,
64
+
65
+ } from "react-router-dom";
66
+
67
+ import axios from "axios";
68
+
69
+ import TopPage from "./Components/Page/TopPage";
70
+
71
+ import TrainingPage from "./Components/Page/TrainingPage";
72
+
73
+ import UserMyPage from "./Components/Page/UserMyPage";
74
+
75
+ import UserList from "./Components/UserList";
76
+
77
+ import UserProfile from "./Components/UserProfile";
78
+
79
+ import UserEdit from "./Components/Page/UserEdit";
80
+
81
+ import SignUp from "./Components/Page/SignUp";
82
+
83
+ import SignIn from "./Components/Page/SignIn";
84
+
85
+ import SearchResultPage from "./Components/Page/SearchResultPage";
86
+
87
+ import Auth from "./Auth";
88
+
89
+ import GymsAndMap from "./Components/GymsAndMap";
90
+
91
+ import GymRegistraion from "./Components/Component/GymRegistration";
92
+
93
+ import Header from "./Components/Header";
94
+
95
+ import NotFound from "./Components/Page/NotFound";
96
+
97
+
98
+
99
+ const Routing = () => {
100
+
101
+ const [loggedInStatus, setLoggedInStatus] = useState(false);
102
+
103
+ const [loginUser, setLoginUser] = useState("");
104
+
105
+ const [isloaded, setIsLoaded] = useState(true);
106
+
107
+
108
+
109
+ const history = useHistory();
110
+
111
+
112
+
113
+ const login = () => {
114
+
115
+ setLoggedInStatus(true);
116
+
117
+ };
118
+
119
+
120
+
121
+ const logout = () => {
122
+
123
+ setLoggedInStatus(false);
124
+
125
+ };
126
+
127
+
128
+
129
+ console.log(loggedInStatus);
130
+
131
+
132
+
133
+ const checkLoginStatus = () => {
134
+
135
+ axios
136
+
137
+ .get("http://localhost:3001/login", { withCredentials: true })
138
+
139
+ .then((response) => {
140
+
141
+ if (response.data.logged_in) {
142
+
143
+ setLoginUser(response.data.user);
144
+
145
+ setLoggedInStatus(true);
146
+
147
+ } else {
148
+
149
+ setLoggedInStatus(false);
150
+
151
+ // history.push("/signin");
152
+
153
+ }
154
+
155
+ })
156
+
157
+ .catch((error) => {
158
+
159
+ console.log("ログインステータスエラー", error);
160
+
161
+ });
162
+
163
+ setIsLoaded(true);
164
+
165
+ };
166
+
167
+
168
+
169
+ useEffect(() => {
170
+
171
+ checkLoginStatus();
172
+
173
+ }, []);
174
+
175
+
176
+
177
+ if (!isloaded) {
178
+
179
+ return <div>読み込み中です</div>;
180
+
181
+ } else {
182
+
183
+ return (
184
+
185
+ <div className="App">
186
+
187
+ <Router>
188
+
189
+ <Header
190
+
191
+ loginUser={loginUser}
192
+
193
+ logout={logout}
194
+
195
+ loggedInStatus={loggedInStatus}
196
+
197
+ />
198
+
199
+ <Switch>
200
+
201
+ <Route
202
+
203
+ exact
204
+
205
+ path={"/signin"}
206
+
207
+ render={(props) => (
208
+
209
+ <SignIn
210
+
211
+ {...props}
212
+
213
+ loggedInStatus={loggedInStatus}
214
+
215
+ login={login}
216
+
217
+ />
218
+
219
+ )}
220
+
221
+ />
222
+
223
+
224
+
225
+ <Auth loggedInStatus={loggedInStatus}>
226
+
227
+ <Switch>
228
+
229
+ <Route exact path="/" component={TopPage} />
230
+
231
+ <Route exact path="/users/:id" component={UserMyPage} />
232
+
233
+
234
+
235
+ <Route
236
+
237
+ exact
238
+
239
+ path={"/users/:id/edit"}
240
+
241
+ render={(props) => (
242
+
243
+ <UserEdit {...props} loginUser={loginUser} />
244
+
245
+ )}
246
+
247
+ />
248
+
249
+ <Route
250
+
251
+ exact
252
+
253
+ path={"/signup"}
254
+
255
+ render={(props) => (
256
+
257
+ <SignUp
258
+
259
+ {...props}
260
+
261
+ loggedInStatus={loggedInStatus}
262
+
263
+ login={login}
264
+
265
+ />
266
+
267
+ )}
268
+
269
+ />
270
+
271
+
272
+
273
+ <Route
274
+
275
+ exact
276
+
277
+ path={"/users"}
278
+
279
+ render={(props) => (
280
+
281
+ <UserList {...props} loggedInStatus={loggedInStatus} />
282
+
283
+ )}
284
+
285
+ />
286
+
287
+
288
+
289
+ <Route exact path="/gyms" component={GymsAndMap} />
290
+
291
+ <Route
292
+
293
+ exact
294
+
295
+ path="/gyms/registration"
296
+
297
+ component={GymRegistraion}
298
+
299
+ />
300
+
301
+ <Route
302
+
303
+ exact
304
+
305
+ path="/users/:userId/trainings/:trainingId"
306
+
307
+ component={TrainingPage}
308
+
309
+ />
310
+
311
+
312
+
313
+ <Route
314
+
315
+ exact
316
+
317
+ path="/searchResult"
318
+
319
+ component={SearchResultPage}
320
+
321
+ />
322
+
323
+
324
+
325
+ <Route component={NotFound} />
326
+
327
+ </Switch>
328
+
329
+ </Auth>
330
+
331
+ </Switch>
332
+
333
+ </Router>
334
+
335
+ </div>
336
+
337
+ );
338
+
339
+ }
340
+
341
+ };
342
+
343
+
344
+
345
+ export default Routing;
346
+
347
+
348
+
349
+ ```