質問編集履歴

1

書式の改善

2021/11/30 01:54

投稿

songman
songman

スコア0

test CHANGED
File without changes
test CHANGED
@@ -4,12 +4,16 @@
4
4
 
5
5
  laravel&Reactでユーザー管理機能の実装をしたいとおもっております。
6
6
 
7
- Reactのコンポーネントなどは作成したのですが、うまく表示がされません。
7
+ Reactのコンポーネントなどは作成したのですが、React-Routeの導入をするとうまく表示がされません。
8
8
 
9
9
  解決策が見つからず、停滞してしまったので、知見を貸していただきたいです。
10
10
 
11
11
 
12
12
 
13
+ React-Routeの機能を実装中下記のようなエラーに遭遇しました。
14
+
15
+
16
+
13
17
  ### 発生している問題・エラーメッセージ
14
18
 
15
19
 
@@ -42,48 +46,324 @@
42
46
 
43
47
 
44
48
 
45
- ### Controllers/Auth/ReactController
46
-
47
-
48
-
49
- ```
50
-
51
- <?php
52
-
53
-
54
-
55
- namespace App\Http\Controllers\Auth;
56
-
57
-
58
-
59
- use App\Http\Controllers\Controller;
60
-
61
- use Illuminate\Http\Request;
62
-
63
-
64
-
65
- class ReactController extends Controller
49
+
50
+
51
+
52
+
53
+ ###route/index.js
54
+
55
+ ```
56
+
57
+ import React from 'react'
58
+
59
+ import { Route, Switch, Redirect } from 'react-router-dom'
60
+
61
+ import NavBar from '../components/NavBar'
62
+
63
+ import CardTemplate from '../components/CardTemplate'
64
+
65
+ import PropTypes from 'prop-types'
66
+
67
+ import { connect } from 'react-redux'
68
+
69
+ import { setSession } from '../actions/authentications'
70
+
71
+
72
+
73
+ const routes = (session)=> {
74
+
75
+ return(
76
+
77
+ <>
78
+
79
+ <NavBar />
80
+
81
+ <div className="py-3">
82
+
83
+ {(()=>{
84
+
85
+ if(session.id===undefined){
86
+
87
+ return (
88
+
89
+ <Switch>
90
+
91
+ <Route exact path="/login" render={() => <CardTemplate title="Login" content="LoginForm" />} />
92
+
93
+ <Route exact path="/register" render={() => <CardTemplate title="Register" content="RegisterForm" />} />
94
+
95
+ <Route exact path="/password/reset" render={() => <CardTemplate title="Reset Password" content="EMailForm" />} />
96
+
97
+ <Route path="/password/reset/:id" render={(props) => <CardTemplate title="Reset Password" content="ResetForm" params={props.match.params} />} />
98
+
99
+ <Redirect to="/login" />
100
+
101
+ </Switch>
102
+
103
+ )
104
+
105
+ }
106
+
107
+ else
108
+
109
+ {
110
+
111
+ if(session.email_verified_at===null)
112
+
113
+ {
114
+
115
+ return (
116
+
117
+ <Switch>
118
+
119
+ <Route exact path="/email/verify" render={() => <CardTemplate title="Verify Your Email Address" content="Verify" />} />
120
+
121
+ <Redirect to="/email/verify" />
122
+
123
+ </Switch>
124
+
125
+ )
126
+
127
+ }
128
+
129
+ else
130
+
131
+ {
132
+
133
+ return (
134
+
135
+ <Switch>
136
+
137
+ <Route exact path="/" render={() => <CardTemplate title="Welcome" content="Welcome" />} />
138
+
139
+ <Route path="/home" render={() => <CardTemplate title="Dashboard" content="Home" />} />
140
+
141
+ <Redirect to="/home" />
142
+
143
+ </Switch>
144
+
145
+ )
146
+
147
+ }
148
+
149
+ }
150
+
151
+ })()}
152
+
153
+ </div>
154
+
155
+ </>
156
+
157
+ )
158
+
159
+ }
160
+
161
+
162
+
163
+ export default routes
164
+
165
+
166
+
167
+ ```
168
+
169
+ ### app.js
170
+
171
+ ```
172
+
173
+ require('./bootstrap');
174
+
175
+
176
+
177
+ import { Provider } from 'react-redux'
178
+
179
+ import React from 'react'
180
+
181
+ import ReactDOM from 'react-dom'
182
+
183
+ import ReactRoot from './ReactRoot'
184
+
185
+ import configureStore, { history } from './stores/configureStore'
186
+
187
+ import { persistStore } from 'redux-persist'
188
+
189
+ import { PersistGate } from 'redux-persist/integration/react'
190
+
191
+
192
+
193
+ const store = configureStore()
194
+
195
+
196
+
197
+ const pstore = persistStore(store)
198
+
199
+
200
+
201
+ const render = (props) => {
202
+
203
+ ReactDOM.render(
204
+
205
+ <Provider store={store}>
206
+
207
+ <PersistGate loading={<p>loading...</p>} persistor={pstore}>
208
+
209
+ <ReactRoot history={history} responseSession={props} />
210
+
211
+ </PersistGate>
212
+
213
+ </Provider>,
214
+
215
+ document.getElementById('react-root')
216
+
217
+ )
218
+
219
+ }
220
+
221
+
222
+
223
+ function authSession()
224
+
225
+ {
226
+
227
+ let params = new URLSearchParams();
228
+
229
+ let url = '/session';
230
+
231
+ window.axios.post(url,params)
232
+
233
+ .then((response)=>{
234
+
235
+ render(response.data)
236
+
237
+ })
238
+
239
+ }
240
+
241
+
242
+
243
+ authSession()
244
+
245
+
246
+
247
+ // page単位でコンポーネントの呼び出し
248
+
249
+ require('./routes/index');
250
+
251
+ require('./pages/Task');
252
+
253
+ require('./pages/Project');
254
+
255
+ require('./pages/MyPage');
256
+
257
+ require('./pages/Example');
258
+
259
+ require('./pages/PostEdit');
260
+
261
+ require('./pages/IconLabelTabs');
262
+
263
+ // require('./pages/SignIn');
264
+
265
+ // require('./pages/SignUp');
266
+
267
+ ```
268
+
269
+
270
+
271
+ ###package.json
272
+
273
+ ```
66
274
 
67
275
  {
68
276
 
277
+ "private": true,
278
+
279
+ "homepage": "./",
280
+
281
+ "scripts": {
282
+
283
+ "dev": "npm run development",
284
+
69
- public function index(Request $request)
285
+ "development": "mix",
286
+
70
-
287
+ "watch": "mix watch",
288
+
289
+ "watch-poll": "mix watch -- --watch-options-poll=1000",
290
+
291
+ "hot": "mix watch --hot",
292
+
293
+ "prod": "npm run production",
294
+
295
+ "production": "mix --production"
296
+
71
- {
297
+ },
72
-
298
+
73
- return view('react');
299
+ "devDependencies": {
300
+
301
+ "@babel/preset-react": "^7.16.0",
302
+
303
+ "axios": "^0.21.4",
304
+
305
+ "bootstrap": "^4.6.1",
306
+
307
+ "connected-react-router": "^6.9.1",
308
+
309
+ "history": "^4.10.1",
310
+
311
+ "jquery": "^3.6",
312
+
313
+ "laravel-mix": "^6.0.39",
314
+
315
+ "lodash": "^4.17.19",
316
+
317
+ "popper.js": "^1.16.1",
318
+
319
+ "postcss": "^8.1.14",
320
+
321
+ "prop-types": "^15.7.2",
322
+
323
+ "react": "^17.0.2",
324
+
325
+ "react-bootstrap": "^2.0.0-rc.1",
326
+
327
+ "react-dom": "^17.0.2",
328
+
329
+ "react-redux": "^7.2.6",
330
+
331
+ "react-router": "^6.0.2",
332
+
333
+ "react-router-dom": "^5.3.0",
334
+
335
+ "redux": "^4.1.2",
336
+
337
+ "redux-persist": "^6.0.0",
338
+
339
+ "resolve-url-loader": "^4.0.0",
340
+
341
+ "sass": "^1.32.11",
342
+
343
+ "sass-loader": "^11.0.1"
344
+
345
+ },
346
+
347
+ "dependencies": {
348
+
349
+ "@emotion/react": "^11.5.0",
350
+
351
+ "@emotion/styled": "^11.3.0",
352
+
353
+ "@material-ui/core": "^4.12.3",
354
+
355
+ "@mui/icons-material": "^5.1.0",
356
+
357
+ "@mui/material": "^5.1.0",
358
+
359
+ "@mui/styled-engine": "^5.1.0",
360
+
361
+ "recharts": "^2.1.6",
362
+
363
+ "styled-components": "^5.3.3"
74
364
 
75
365
  }
76
366
 
77
-
78
-
79
- public function session(Request $request)
80
-
81
- {
82
-
83
- return $request-> user();
84
-
85
- }
86
-
87
367
  }
88
368
 
89
369
 
@@ -92,451 +372,9 @@
92
372
 
93
373
 
94
374
 
95
- ###route
96
-
97
- ```
98
-
99
- <?php
100
-
101
-
102
-
103
- use Illuminate\Support\Facades\Route;
104
-
105
-
106
-
107
- Route::get('/', function () {
108
-
109
- return view('welcome');
110
-
111
- });
112
-
113
-
114
-
115
- Auth::routes(['verify' => true]);
116
-
117
-
118
-
119
- Route::get('/login', 'App\Http\Controllers\Auth\ReactController@index')->name('login');
120
-
121
- Route::get('/register', 'App\Http\Controllers\Auth\ReactController@index')->name('register');
122
-
123
- Route::get('password/reset', 'App\Http\Controllers\Auth\ReactController@index')->name('password.request');
124
-
125
- Route::get('password/reset/{token}', 'App\Http\Controllers\Auth\ReactController@index')->name('password.reset');
126
-
127
- Route::get('email/verify', 'App\Http\Controllers\Auth\ReactController@index')->name('verification.notice');
128
-
129
-
130
-
131
- Route::post('/session', 'App\Http\Controllers\Auth\ReactController@session')->name('session');
132
-
133
- Route::get('/{router}', 'App\Http\Controllers\Auth\ReactController@index')->name('home');
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
- Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
142
-
143
- ```
144
-
145
-
146
-
147
- ###Veiw
148
-
149
- ```
150
-
151
- <!DOCTYPE html>
152
-
153
- <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
154
-
155
- <head>
156
-
157
- <meta charset="utf-8">
158
-
159
- <meta name="viewport" content="width=device-width, initial-scale=1">
160
-
161
-
162
-
163
- <!-- CSRF Token -->
164
-
165
- <meta name="csrf-token" content="{{ csrf_token() }}">
166
-
167
-
168
-
169
- <title>{{ config('app.name', 'Laravel') }}</title>
170
-
171
-
172
-
173
- <!-- Fonts -->
174
-
175
- <link rel="dns-prefetch" href="//fonts.gstatic.com">
176
-
177
- <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">
178
-
179
-
180
-
181
- <!-- Styles -->
182
-
183
- <link href="{{ asset('css/app.css') }}" rel="stylesheet">
184
-
185
- </head>
186
-
187
- <body>
188
-
189
- <div id="react-root"></div>
190
-
191
- <script>
192
-
193
- var laravelSession = {};
194
-
195
- laravelSession['status']=@if(session('status'))'{{session('status')}}'@else''@endif;
196
-
197
- laravelSession['resent']=@if(session('resent'))'{{session('resent')}}'@else''@endif;
198
-
199
- var laravelErrors=@php print(htmlspecialchars_decode($errors))@endphp;
200
-
201
- </script>
202
-
203
- <!-- Scripts -->
204
-
205
- <script src="{{ asset('js/app.js') }}" defer></script>
206
-
207
- </body>
208
-
209
- </html>
210
-
211
- ```
212
-
213
-
214
-
215
- ###route/index.js
216
-
217
- ```
218
-
219
- import React from 'react'
220
-
221
- import { Route, Switch, Redirect } from 'react-router-dom'
222
-
223
- import NavBar from '../components/NavBar'
224
-
225
- import CardTemplate from '../components/CardTemplate'
226
-
227
- import PropTypes from 'prop-types'
228
-
229
- import { connect } from 'react-redux'
230
-
231
- import { setSession } from '../actions/authentications'
232
-
233
-
234
-
235
- const routes = (session)=> {
236
-
237
- return(
238
-
239
- <>
240
-
241
- <NavBar />
242
-
243
- <div className="py-3">
244
-
245
- {(()=>{
246
-
247
- if(session.id===undefined){
248
-
249
- return (
250
-
251
- <Switch>
252
-
253
- <Route exact path="/login" render={() => <CardTemplate title="Login" content="LoginForm" />} />
254
-
255
- <Route exact path="/register" render={() => <CardTemplate title="Register" content="RegisterForm" />} />
256
-
257
- <Route exact path="/password/reset" render={() => <CardTemplate title="Reset Password" content="EMailForm" />} />
258
-
259
- <Route path="/password/reset/:id" render={(props) => <CardTemplate title="Reset Password" content="ResetForm" params={props.match.params} />} />
260
-
261
- <Redirect to="/login" />
262
-
263
- </Switch>
264
-
265
- )
266
-
267
- }
268
-
269
- else
270
-
271
- {
272
-
273
- if(session.email_verified_at===null)
274
-
275
- {
276
-
277
- return (
278
-
279
- <Switch>
280
-
281
- <Route exact path="/email/verify" render={() => <CardTemplate title="Verify Your Email Address" content="Verify" />} />
282
-
283
- <Redirect to="/email/verify" />
284
-
285
- </Switch>
286
-
287
- )
288
-
289
- }
290
-
291
- else
292
-
293
- {
294
-
295
- return (
296
-
297
- <Switch>
298
-
299
- <Route exact path="/" render={() => <CardTemplate title="Welcome" content="Welcome" />} />
300
-
301
- <Route path="/home" render={() => <CardTemplate title="Dashboard" content="Home" />} />
302
-
303
- <Redirect to="/home" />
304
-
305
- </Switch>
306
-
307
- )
308
-
309
- }
310
-
311
- }
312
-
313
- })()}
314
-
315
- </div>
316
-
317
- </>
318
-
319
- )
320
-
321
- }
322
-
323
-
324
-
325
- export default routes
326
-
327
-
328
-
329
- ```
330
-
331
- ### app.js
332
-
333
- ```
334
-
335
- require('./bootstrap');
336
-
337
-
338
-
339
- import { Provider } from 'react-redux'
340
-
341
- import React from 'react'
342
-
343
- import ReactDOM from 'react-dom'
344
-
345
- import ReactRoot from './ReactRoot'
346
-
347
- import configureStore, { history } from './stores/configureStore'
348
-
349
- import { persistStore } from 'redux-persist'
350
-
351
- import { PersistGate } from 'redux-persist/integration/react'
352
-
353
-
354
-
355
- const store = configureStore()
356
-
357
-
358
-
359
- const pstore = persistStore(store)
360
-
361
-
362
-
363
- const render = (props) => {
364
-
365
- ReactDOM.render(
366
-
367
- <Provider store={store}>
368
-
369
- <PersistGate loading={<p>loading...</p>} persistor={pstore}>
370
-
371
- <ReactRoot history={history} responseSession={props} />
372
-
373
- </PersistGate>
374
-
375
- </Provider>,
376
-
377
- document.getElementById('react-root')
378
-
379
- )
380
-
381
- }
382
-
383
-
384
-
385
- function authSession()
386
-
387
- {
388
-
389
- let params = new URLSearchParams();
390
-
391
- let url = '/session';
392
-
393
- window.axios.post(url,params)
394
-
395
- .then((response)=>{
396
-
397
- render(response.data)
398
-
399
- })
400
-
401
- }
402
-
403
-
404
-
405
- authSession()
406
-
407
-
408
-
409
- // page単位でコンポーネントの呼び出し
410
-
411
- require('./routes/index');
412
-
413
- require('./pages/Task');
414
-
415
- require('./pages/Project');
416
-
417
- require('./pages/MyPage');
418
-
419
- require('./pages/Example');
420
-
421
- require('./pages/PostEdit');
422
-
423
- require('./pages/IconLabelTabs');
424
-
425
- // require('./pages/SignIn');
426
-
427
- // require('./pages/SignUp');
428
-
429
- ```
430
-
431
-
432
-
433
- ###package.json
434
-
435
- ```
436
-
437
- {
438
-
439
- "private": true,
440
-
441
- "homepage": "./",
442
-
443
- "scripts": {
444
-
445
- "dev": "npm run development",
446
-
447
- "development": "mix",
448
-
449
- "watch": "mix watch",
450
-
451
- "watch-poll": "mix watch -- --watch-options-poll=1000",
452
-
453
- "hot": "mix watch --hot",
454
-
455
- "prod": "npm run production",
456
-
457
- "production": "mix --production"
458
-
459
- },
460
-
461
- "devDependencies": {
462
-
463
- "@babel/preset-react": "^7.16.0",
464
-
465
- "axios": "^0.21.4",
466
-
467
- "bootstrap": "^4.6.1",
468
-
469
- "connected-react-router": "^6.9.1",
470
-
471
- "history": "^4.10.1",
472
-
473
- "jquery": "^3.6",
474
-
475
- "laravel-mix": "^6.0.39",
476
-
477
- "lodash": "^4.17.19",
478
-
479
- "popper.js": "^1.16.1",
480
-
481
- "postcss": "^8.1.14",
482
-
483
- "prop-types": "^15.7.2",
484
-
485
- "react": "^17.0.2",
486
-
487
- "react-bootstrap": "^2.0.0-rc.1",
488
-
489
- "react-dom": "^17.0.2",
490
-
491
- "react-redux": "^7.2.6",
492
-
493
- "react-router": "^6.0.2",
494
-
495
- "react-router-dom": "^5.3.0",
496
-
497
- "redux": "^4.1.2",
498
-
499
- "redux-persist": "^6.0.0",
500
-
501
- "resolve-url-loader": "^4.0.0",
502
-
503
- "sass": "^1.32.11",
504
-
505
- "sass-loader": "^11.0.1"
506
-
507
- },
508
-
509
- "dependencies": {
510
-
511
- "@emotion/react": "^11.5.0",
512
-
513
- "@emotion/styled": "^11.3.0",
514
-
515
- "@material-ui/core": "^4.12.3",
516
-
517
- "@mui/icons-material": "^5.1.0",
518
-
519
- "@mui/material": "^5.1.0",
520
-
521
- "@mui/styled-engine": "^5.1.0",
522
-
523
- "recharts": "^2.1.6",
524
-
525
- "styled-components": "^5.3.3"
526
-
527
- }
528
-
529
- }
530
-
531
-
532
-
533
- ```
534
-
535
-
536
-
537
375
  ###試したこと
538
376
 
539
- Routeなどのバージョンを再度見直し、React-router-domでV6を使用していたが、他のエラーが発生したので、バージョンを変更した。
377
+ Routeなどのバージョンを再度見直し、React-router-domでV6を使用していたが、他のエラーが発生したので、バージョンを変更しました。
540
378
 
541
379
 
542
380
 
@@ -544,11 +382,11 @@
544
382
 
545
383
  ###聞きたいこと
546
384
 
547
- ・バージョンなどの関係があるのでしょうか。
385
+ ・バージョンの依存関係があるのでしょうか。
548
-
386
+
549
- ・エラーの解決方法
387
+ コンポーネントの作成時には問題なく表示されていたのですが、React-Routeの機能を使うとどうしてもエラーがでてきてしまいます
550
-
388
+
551
- ・ユーザー管理機能で参考のものがありましたら、教えていただきたいです。
389
+ もしユーザー管理機能で参考のものがありましたら、教えていただきたいです。
552
390
 
553
391
 
554
392