質問編集履歴

3

進捗分更新

2020/12/17 00:50

投稿

yuki-o0413
yuki-o0413

スコア8

test CHANGED
@@ -1 +1 @@
1
- Reduxにて、フォームに入力した内容がテーブルに出力されるようにしたい
1
+ Reduxにて、フォームに入力した内容が登録されるようにしたい
test CHANGED
@@ -1,38 +1,42 @@
1
1
  フォームにて、入力した内容がlogで出力されるようにしたいが、
2
2
 
3
- エラーが出てしまいます。
3
+ 表示画面にて入力を行うとエラーが出てしまいます。
4
4
 
5
5
  エラー内容
6
6
 
7
7
  ```
8
8
 
9
- Content.js:57 Uncaught TypeError: Cannot read property 'id' of undefined
9
+ App.js:25 Uncaught TypeError: _actions__WEBPACK_IMPORTED_MODULE_1__.updateFormId is not a function
10
+
10
-
11
+ at Object.updateFormId (App.js:25)
12
+
13
+ at Object.onChangeId (App.js:21)
14
+
11
- at Content (Content.js:57)
15
+ at onChange (Content.js:36)
16
+
12
-
17
+   ・・・
18
+
13
- ```
19
+ ```
14
-
20
+
15
- ```
21
+ ```
16
-
22
+
17
- react-dom.development.js:19814 The above error occurred in the <Content> component:
23
+ Uncaught TypeError: this.props.updateFormName is not a function
24
+
18
-
25
+ at Object.onChangeName (App.js:25)
26
+
19
- in Content (created by App)
27
+ at onChange (Content.js:43)
20
-
28
+
21
- ```
29
+ ```
22
-
23
- ```
30
+
24
-
25
- Content.js:57 Uncaught TypeError: Cannot read property 'id' of undefined
26
-
27
- at Content (Content.js:57)
28
-
29
- ```
30
-
31
- idが定義されていないと言われていますが、どう直せば良いのか分からず、進めないでいます。
31
+ updateFormIdとupdateFormName関数ではないと言われていますが、具体的にどう直せば良いのか分からず、進めないでいます。
32
+
33
+
34
+
32
-
35
+ React、JavaScript学習初めて1ヶ月の初心者です。
36
+
33
-
37
+ サンプルコードを真似しつつコードを書いてます。
34
-
38
+
35
- React、JavaScript初心者です。改善点、問題解決のためのアプローチの方法、アドバイスいただけたらありがたいです。お願いします。
39
+ 改善点、問題解決のためのアプローチの方法、アドバイスいただけたらありがたいです。お願いします。
36
40
 
37
41
  また、他に必要な情報があればあ追記します。
38
42
 
@@ -42,7 +46,7 @@
42
46
 
43
47
  'use strict';
44
48
 
45
- import React from 'react';
49
+ import React,{ useState } from 'react';
46
50
 
47
51
  import { createStore } from "redux";
48
52
 
@@ -50,328 +54,374 @@
50
54
 
51
55
  export default function Content(props) {
52
56
 
53
- // 変数を定義、空の配列を作っている
54
-
55
- const ids = [];
56
-
57
- const names = [];
58
-
59
- const pvs = [];
60
-
61
-
62
-
63
- const reducer = () => {
64
-
65
- console.log("reducer has been called.");
66
-
67
- }
68
-
69
-
70
-
71
- const store = createStore(reducer, 1);
72
-
73
-
74
-
75
- store.subscribe(() => {
76
-
77
- console.log("store changed", store.getState());
78
-
79
- });
80
-
81
-
82
-
83
- store.dispatch({type: "INC"});
84
-
85
-
86
-
87
- // ループでテーブルに順に入れ込むためのコード
88
-
89
- for ( let i = 1; i <= 10; i++ ) {
90
-
91
- ids.push(i)
92
-
93
- names.push("name" + i)
94
-
95
- pvs.push(Math.floor( Math.random() * 1000 ))
96
-
97
- }
98
-
99
-
100
-
101
- // ボディのテーブルに入れ込むための変数用意
102
-
103
- console.log(pvs);
104
-
105
- const tbody = ids.map((id,index)=>{
106
-
107
- return(
108
-
109
- <tr key={index}>
110
-
111
- <td>{id}</td><td>{ names[index] }</td><td>{pvs[index]}</td>
112
-
113
- </tr>
57
+ const ids = props.formItem.id;
58
+
59
+ const names = props.formItem.name;
60
+
61
+ console.log(props.formItem.id["list"])
62
+
63
+ // const ids = props.formItem.id;
64
+
65
+ console.log(ids)
66
+
67
+ return (
68
+
69
+ <>
70
+
71
+ <p>content</p>
72
+
73
+
74
+
75
+ <Form
76
+
77
+ onChangeId={props.onChangeId}
78
+
79
+ onChangeName={props.onChangeName}
80
+
81
+ />
82
+
83
+ <button type="button"
84
+
85
+ className="btn btn-outline-success"
86
+
87
+ onClick={() => props.onSaveItem()} >
88
+
89
+ SAVE
90
+
91
+ </button>
92
+
93
+ </>
94
+
95
+ );
96
+
97
+ }
98
+
99
+
100
+
101
+ function Form(props) {
102
+
103
+ return (
104
+
105
+ <div className="form-group">
106
+
107
+ <label className="pt-2">ID</label>
108
+
109
+ <input id="1"
110
+
111
+ type="text"
112
+
113
+ className="form-control"
114
+
115
+ value={props.id}
116
+
117
+ onChange={(e) => props.onChangeId(e)}
118
+
119
+ />
120
+
121
+ <label className="pt-2">Name</label>
122
+
123
+ <input id="2"
124
+
125
+ type="text"
126
+
127
+ className="form-control"
128
+
129
+ value={props.name}
130
+
131
+ onChange={(e) => props.onChangeName(e)}
132
+
133
+ />
134
+
135
+ </div>
136
+
137
+ )
138
+
139
+ }
140
+
141
+ ```
142
+
143
+
144
+
145
+ ```App.js
146
+
147
+ import React, { Component } from 'react';
148
+
149
+
150
+
151
+ import Header from './components/Header';
152
+
153
+ import Footer from './components/Footer';
154
+
155
+ import Top from './components/Top';
156
+
157
+ import Content from './components/Content';
158
+
159
+
160
+
161
+
162
+
163
+ class App extends Component {
164
+
165
+ constructor(props) {
166
+
167
+ super(props);
168
+
169
+ }
170
+
171
+
172
+
173
+ componentDidMount() {
174
+
175
+ const id = 1;
176
+
177
+ const name = "name1";
178
+
179
+ this.props.didMnt(id,name);
180
+
181
+ }
182
+
183
+ // formId
184
+
185
+ onChangeId = (e) => {
186
+
187
+ this.props.updateFormId(e.target.id);
188
+
189
+ }
190
+
191
+ // forName
192
+
193
+ onChangeName = (e) => {
194
+
195
+ this.props.updateFormName(e.target.name);
196
+
197
+ }
198
+
199
+ // clickSaveButton
200
+
201
+ onSaveItem = () => {
202
+
203
+ console.log("SAVE_ID: ",this.props.conditions.id);
204
+
205
+ console.log("SAVE_NAME: ",this.props.conditions.name);
206
+
207
+ }
208
+
209
+
210
+
211
+ render() {
212
+
213
+
214
+
215
+ console.log(this.props);
216
+
217
+ const formItem = this.props
218
+
219
+ // formEvent
220
+
221
+ const contentHandler = ({onChangeId, onChangeName, onSaveItem}) => ({ onChangeId, onChangeName, onSaveItem })
222
+
223
+
224
+
225
+ console.log('App.render:', this.props);
226
+
227
+
228
+
229
+ return (
230
+
231
+ <div className="content">
232
+
233
+ <Header />
234
+
235
+ sample site
236
+
237
+ {/* <div className="row">
238
+
239
+
240
+
241
+ <div className="col-5">
242
+
243
+ <p>test</p>
244
+
245
+ </div>
246
+
247
+ </div> */}
248
+
249
+ <Top />
250
+
251
+ <br />
252
+
253
+ <br />
254
+
255
+ <br />
256
+
257
+ <div className="container">
258
+
259
+ <Content formItem={formItem} {...contentHandler(this)} />
260
+
261
+ </div>
262
+
263
+ <br />
264
+
265
+ <br />
266
+
267
+ <br />
268
+
269
+ <Footer />
270
+
271
+ </div>
114
272
 
115
273
  );
116
274
 
117
- });
118
-
119
-
120
-
121
- return (
122
-
123
- <>
124
-
125
- <h1>
126
-
127
- Redux課題やってます
128
-
129
- </h1>
130
-
131
- <table className="table">
132
-
133
- <thead className="table-dark">
134
-
135
- <tr>
136
-
137
- <th scope="col-2">id</th>
138
-
139
- <th scope="col-4">name</th>
140
-
141
- <th scope="col-6">pv</th>
142
-
143
- </tr>
144
-
145
- </thead>
146
-
147
- <tbody>
148
-
149
- { tbody }
150
-
151
- </tbody>
152
-
153
- </table>
154
-
155
- </>
275
+ }
276
+
277
+ }
278
+
279
+ export default App;
280
+
281
+ ```
282
+
283
+
284
+
285
+ ```containers/App.js
286
+
287
+ import { connect } from 'react-redux';
288
+
289
+ import * as actions from '../actions';
290
+
291
+ import App from '../App';
292
+
293
+
294
+
295
+ const mapStateToProps = state => {
296
+
297
+ const {id,name} = state;
298
+
299
+ return {id,name}
300
+
301
+ }
302
+
303
+
304
+
305
+ const mapDispatchToProps = dispatch => {
306
+
307
+ return {
308
+
309
+ didMnt: (id,name) => dispatch(actions.didMnt(id,name)),
310
+
311
+ updateFormId: (id) => dispatch(actions.updateFormId(id)),
312
+
313
+ }
314
+
315
+ }
316
+
317
+ export default connect(mapStateToProps, mapDispatchToProps)(App)
318
+
319
+ ```
320
+
321
+
322
+
323
+
324
+
325
+ ```createStore.js
326
+
327
+ import { createStore as reduxCreateStore, combineReducers } from "redux";
328
+
329
+
330
+
331
+ export default function createStore() {
332
+
333
+ const store = reduxCreateStore(
334
+
335
+ combineReducers({
336
+
337
+ id,
338
+
339
+ name
340
+
341
+ })
156
342
 
157
343
  );
158
344
 
159
- }
160
-
161
-
162
-
163
- export default function LoginForm(props) {
164
-
165
- return(
166
-
167
- <div className="card shadow-sm">
168
-
169
- <div className="card-body">
170
-
171
- <form>
172
-
173
- <div className="form-group">
174
-
175
- <label htmlFor="inputName" className="col-form-label">name</label>
176
-
177
- <input type="text"
178
-
179
- className="form-control"
180
-
181
- id="inputName"
182
-
183
- placeholder="name"
184
-
185
- value={props.authenticationDetails.Name}
186
-
187
- onChange={(e) => props.onChangeInputName(e)} />
188
-
189
- </div>
190
-
191
- </form>
192
-
193
- </div>
194
-
195
- </div>
196
-
197
- )
198
-
199
- }
200
-
201
- ```
202
-
203
-
204
-
205
- ```App.js
206
-
207
- import React, { Component } from 'react';
208
-
209
-
210
-
211
- import Header from './components/Header';
212
-
213
- import Footer from './components/Footer';
214
-
215
- import Top from './components/Top';
216
-
217
- import Content from './components/Content';
218
-
219
-
220
-
221
-
222
-
223
- class App extends Component {
224
-
225
- constructor(props) {
226
-
227
- super(props);
228
-
229
- }
230
-
231
- componentDidMount() {
232
-
233
- const id = 1
234
-
235
- const name = "name"
236
-
237
- this.props.didMnt(id,name);
238
-
239
- }
240
-
241
-
242
-
243
- onChangeId = (e) => {
244
-
245
- this.props.updateFormId(e.target.value);
246
-
247
- }
248
-
249
- // forName
250
-
251
- onChangeName = (e) => {
252
-
253
- this.props.updateFormName(e.target.value);
254
-
255
- }
256
-
257
-
258
-
259
- onSaveItem = () => {
260
-
261
- console.log("SAVE_ID: ",this.props.conditions.id);
262
-
263
- console.log("SAVE_NAME: ",this.props.conditions.name);
264
-
265
- }
266
-
267
-
268
-
269
- componentDidMount() {
270
-
271
- (async () => {
272
-
273
- const test = "test"
274
-
275
-
276
-
277
- this.props.didMnt(test);
278
-
279
- })();
280
-
281
- }
282
-
283
-
284
-
285
- render() {
286
-
287
- console.log('App.render:', this.props);
288
-
289
-
290
-
291
- const data1 = "AAAA";
292
-
293
- const data2 = "data2";
294
-
295
-
296
-
297
-
298
-
299
- const topData = ({data1}) => ({data1})
300
-
301
-
302
-
303
-
304
-
305
- const contentData = ({data2}) => ({data2})
306
-
307
- const ja = "*****Japan!";
308
-
309
-
310
-
311
- const formItem = this.props
312
-
313
- // formEvent
314
-
315
- const contentHandler = ({onChangeId, onChangeName}) => ({onChangeId, onChangeName})
316
-
317
-
318
-
319
- return (
320
-
321
- <div className="content">
322
-
323
- <Header />
324
-
325
- sample site
326
-
327
- <div className="row">
328
-
329
- <div className="col-3">
330
-
331
- <h1>{ja}</h1>
332
-
333
- </div>
334
-
335
- <div className="col-5">
336
-
337
- <p>test</p>
338
-
339
- </div>
340
-
341
- </div>
342
-
343
- <Top data1={data1} />
344
-
345
- <br />
346
-
347
- <br />
348
-
349
- <br />
350
-
351
- <div className="container">
352
-
353
- <Content formItem={formItem} {...contentHandler(this)}/>
354
-
355
-
356
-
357
- </div>
358
-
359
- <br />
360
-
361
- <br />
362
-
363
- <br />
364
-
365
- </div>
366
-
367
- );
368
-
369
- }
370
-
371
- }
372
-
373
- export default App;
374
-
375
-
376
-
377
- ```
345
+ return store;
346
+
347
+ }
348
+
349
+ ```
350
+
351
+ ```actions.js
352
+
353
+ export const didMnt = (id, name) => {
354
+
355
+ return {
356
+
357
+ type: 'DID_MOUNT',
358
+
359
+ payload: { id, name }
360
+
361
+ };
362
+
363
+ }
364
+
365
+ ```
366
+
367
+
368
+
369
+ ```reducers.js
370
+
371
+
372
+
373
+ const initialState = {
374
+
375
+ list: -1
376
+
377
+ }
378
+
379
+ export function id(state = initialState, action) {
380
+
381
+ const newState = JSON.parse(JSON.stringify(state));
382
+
383
+ switch (action.type) {
384
+
385
+ case 'DID_MOUNT':
386
+
387
+ newState.list = action.payload.id;
388
+
389
+ break;
390
+
391
+ default:
392
+
393
+ return state;
394
+
395
+ }
396
+
397
+ return newState;
398
+
399
+ };
400
+
401
+ export function name(state = initialState, action) {
402
+
403
+ const newState = JSON.parse(JSON.stringify(state));
404
+
405
+ switch (action.type) {
406
+
407
+ case 'DID_MOUNT':
408
+
409
+ newState.list = action.payload.name;
410
+
411
+ break;
412
+
413
+ default:
414
+
415
+ return state;
416
+
417
+ }
418
+
419
+ return newState;
420
+
421
+ };
422
+
423
+
424
+
425
+
426
+
427
+ ```

2

エラーの内容修正

2020/12/17 00:50

投稿

yuki-o0413
yuki-o0413

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,34 +1,56 @@
1
- Reduxを使い、フォーム入力した内容がテーブルに登録されるようにしたいが、
1
+ フォームにて、入力した内容がlogで出力されるようにしたいが、
2
-
2
+
3
- handleChange、handleSubmitでエラーが出てしまいます。
3
+ でエラーが出てしまいます。
4
-
5
-
6
4
 
7
5
  エラー内容
8
6
 
7
+ ```
8
+
9
+ Content.js:57 Uncaught TypeError: Cannot read property 'id' of undefined
10
+
9
- 「';' が必要です。ts(1005)
11
+ at Content (Content.js:57)
12
+
10
-
13
+ ```
14
+
15
+ ```
16
+
17
+ react-dom.development.js:19814 The above error occurred in the <Content> component:
18
+
19
+ in Content (created by App)
20
+
21
+ ```
22
+
23
+ ```
24
+
25
+ Content.js:57 Uncaught TypeError: Cannot read property 'id' of undefined
26
+
11
- →セミコロンをつけてもエラーは解決できませんでした
27
+ at Content (Content.js:57)
28
+
29
+ ```
30
+
31
+ ・idが定義されていないと言われていますが、どう直せば良いのか分からず、進めないでいます。
12
32
 
13
33
 
14
34
 
15
35
  React、JavaScript初心者です。改善点、問題解決のためのアプローチの方法、アドバイスいただけたらありがたいです。お願いします。
16
36
 
17
-
37
+ また、他に必要な情報があればあ追記します。
18
-
38
+
39
+
40
+
19
- ```JavaScript
41
+ ```Content.js
20
42
 
21
43
  'use strict';
22
44
 
23
45
  import React from 'react';
24
46
 
25
-
47
+ import { createStore } from "redux";
26
48
 
27
49
 
28
50
 
29
51
  export default function Content(props) {
30
52
 
31
- // 変数を定義
53
+ // 変数を定義、空の配列を作っている
32
54
 
33
55
  const ids = [];
34
56
 
@@ -38,29 +60,27 @@
38
60
 
39
61
 
40
62
 
41
- this.state = {value: ''};
42
-
43
-
44
-
45
- this.handleChange = this.handleChange.bind(this);
46
-
47
- this.handleSubmit = this.handleSubmit.bind(this);
48
-
49
-
50
-
51
- handleChange(event) {
63
+ const reducer = () => {
52
-
64
+
53
- this.setState({value: event.target.value});
65
+ console.log("reducer has been called.");
54
-
66
+
55
- }
67
+ }
68
+
69
+
70
+
56
-
71
+ const store = createStore(reducer, 1);
72
+
73
+
74
+
57
- handleSubmit(event) {
75
+ store.subscribe(() => {
58
-
76
+
59
- alert('A name was submitted: ' + this.state.value);
77
+ console.log("store changed", store.getState());
60
-
61
- event.preventDefault();
78
+
62
-
63
- }
79
+ });
80
+
81
+
82
+
83
+ store.dispatch({type: "INC"});
64
84
 
65
85
 
66
86
 
@@ -88,7 +108,7 @@
88
108
 
89
109
  <tr key={index}>
90
110
 
91
- <td>{id}</td>{ names[index] }<td>{pvs[index]}</td>
111
+ <td>{id}</td><td>{ names[index] }</td><td>{pvs[index]}</td>
92
112
 
93
113
  </tr>
94
114
 
@@ -104,7 +124,7 @@
104
124
 
105
125
  <h1>
106
126
 
107
- Redux課題やってます・・・
127
+ Redux課題やってます
108
128
 
109
129
  </h1>
110
130
 
@@ -130,24 +150,228 @@
130
150
 
131
151
  </tbody>
132
152
 
133
- <form onSubmit={this.handleSubmit}>
134
-
135
- <label>
136
-
137
- name:
138
-
139
- </label>
153
+ </table>
154
+
140
-
155
+ </>
156
+
157
+ );
158
+
159
+ }
160
+
161
+
162
+
163
+ export default function LoginForm(props) {
164
+
165
+ return(
166
+
167
+ <div className="card shadow-sm">
168
+
169
+ <div className="card-body">
170
+
171
+ <form>
172
+
173
+ <div className="form-group">
174
+
141
- <input type="text" value={this.state.value} onChange={this.handleChange} />
175
+ <label htmlFor="inputName" className="col-form-label">name</label>
176
+
177
+ <input type="text"
178
+
179
+ className="form-control"
180
+
181
+ id="inputName"
182
+
183
+ placeholder="name"
184
+
185
+ value={props.authenticationDetails.Name}
186
+
187
+ onChange={(e) => props.onChangeInputName(e)} />
188
+
189
+ </div>
142
190
 
143
191
  </form>
144
192
 
145
- </table>
146
-
147
- </>
193
+ </div>
194
+
148
-
195
+ </div>
196
+
149
- );
197
+ )
150
198
 
151
199
  }
152
200
 
153
201
  ```
202
+
203
+
204
+
205
+ ```App.js
206
+
207
+ import React, { Component } from 'react';
208
+
209
+
210
+
211
+ import Header from './components/Header';
212
+
213
+ import Footer from './components/Footer';
214
+
215
+ import Top from './components/Top';
216
+
217
+ import Content from './components/Content';
218
+
219
+
220
+
221
+
222
+
223
+ class App extends Component {
224
+
225
+ constructor(props) {
226
+
227
+ super(props);
228
+
229
+ }
230
+
231
+ componentDidMount() {
232
+
233
+ const id = 1
234
+
235
+ const name = "name"
236
+
237
+ this.props.didMnt(id,name);
238
+
239
+ }
240
+
241
+
242
+
243
+ onChangeId = (e) => {
244
+
245
+ this.props.updateFormId(e.target.value);
246
+
247
+ }
248
+
249
+ // forName
250
+
251
+ onChangeName = (e) => {
252
+
253
+ this.props.updateFormName(e.target.value);
254
+
255
+ }
256
+
257
+
258
+
259
+ onSaveItem = () => {
260
+
261
+ console.log("SAVE_ID: ",this.props.conditions.id);
262
+
263
+ console.log("SAVE_NAME: ",this.props.conditions.name);
264
+
265
+ }
266
+
267
+
268
+
269
+ componentDidMount() {
270
+
271
+ (async () => {
272
+
273
+ const test = "test"
274
+
275
+
276
+
277
+ this.props.didMnt(test);
278
+
279
+ })();
280
+
281
+ }
282
+
283
+
284
+
285
+ render() {
286
+
287
+ console.log('App.render:', this.props);
288
+
289
+
290
+
291
+ const data1 = "AAAA";
292
+
293
+ const data2 = "data2";
294
+
295
+
296
+
297
+
298
+
299
+ const topData = ({data1}) => ({data1})
300
+
301
+
302
+
303
+
304
+
305
+ const contentData = ({data2}) => ({data2})
306
+
307
+ const ja = "*****Japan!";
308
+
309
+
310
+
311
+ const formItem = this.props
312
+
313
+ // formEvent
314
+
315
+ const contentHandler = ({onChangeId, onChangeName}) => ({onChangeId, onChangeName})
316
+
317
+
318
+
319
+ return (
320
+
321
+ <div className="content">
322
+
323
+ <Header />
324
+
325
+ sample site
326
+
327
+ <div className="row">
328
+
329
+ <div className="col-3">
330
+
331
+ <h1>{ja}</h1>
332
+
333
+ </div>
334
+
335
+ <div className="col-5">
336
+
337
+ <p>test</p>
338
+
339
+ </div>
340
+
341
+ </div>
342
+
343
+ <Top data1={data1} />
344
+
345
+ <br />
346
+
347
+ <br />
348
+
349
+ <br />
350
+
351
+ <div className="container">
352
+
353
+ <Content formItem={formItem} {...contentHandler(this)}/>
354
+
355
+
356
+
357
+ </div>
358
+
359
+ <br />
360
+
361
+ <br />
362
+
363
+ <br />
364
+
365
+ </div>
366
+
367
+ );
368
+
369
+ }
370
+
371
+ }
372
+
373
+ export default App;
374
+
375
+
376
+
377
+ ```

1

エラー文追加

2020/12/07 17:26

投稿

yuki-o0413
yuki-o0413

スコア8

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,15 @@
4
4
 
5
5
 
6
6
 
7
+ エラー内容
8
+
9
+ 「';' が必要です。ts(1005)」
10
+
11
+ →セミコロンをつけてもエラーは解決できませんでした
12
+
13
+
14
+
7
- React、JavaScript初心者です。改善点、アプローチの方法、アドバイスいただけたらありがたいです。お願いします。
15
+ React、JavaScript初心者です。改善点、問題解決のためのアプローチの方法、アドバイスいただけたらありがたいです。お願いします。
8
16
 
9
17
 
10
18