質問編集履歴

1

propsを親コンポーネントから子コンポーネントに受け渡すようにしたところ、別のエラーが発生しました。

2019/12/04 09:28

投稿

chigu4ps5
chigu4ps5

スコア7

test CHANGED
File without changes
test CHANGED
@@ -54,19 +54,105 @@
54
54
 
55
55
  id: uniquId
56
56
 
57
+ };
58
+
59
+
60
+
61
+ let add = tasks.concat(task)
62
+
63
+
64
+
65
+ this.setState({
66
+
67
+ tasks: add,
68
+
69
+ uniquId: uniquId + 1
70
+
71
+ });
72
+
73
+ }
74
+
75
+
76
+
77
+ deleteTodo(i) {
78
+
79
+ let deleTask = this.state.tasks.splice(i, 1);
80
+
81
+ this.setState({
82
+
83
+ tasks: deleTask
84
+
85
+ });
86
+
87
+ }
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+ render() {
96
+
97
+ return (
98
+
99
+ <div>
100
+
101
+ <h1>Todoリスト</h1>
102
+
103
+ <TodoList tasks={this.state.tasks} deleteTodo={this.deleteTodo}/>
104
+
105
+ <TodoAdd addTask={this.addTask}/>
106
+
107
+ </div>
108
+
109
+ );
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+ export default App;
118
+
119
+ ```
120
+
121
+
122
+
123
+ `TodoAdd`
124
+
125
+ ```
126
+
127
+ import React, { Component } from 'react';
128
+
129
+
130
+
131
+ class TodoAdd extends React.Component {
132
+
133
+ constructor(props) {
134
+
135
+ super(props);
136
+
137
+ this.state = {
138
+
139
+ inputValue: null,
140
+
57
141
  }
58
142
 
59
-
143
+ this.handleChange = this.handleChange.bind(this);
60
-
144
+
61
- let add = tasks.concat(task)
145
+ this.handleClick = this.handleClick.bind(this);
146
+
62
-
147
+ }
148
+
149
+
150
+
63
-
151
+ handleChange(e) {
64
152
 
65
153
  this.setState({
66
154
 
67
- tasks: add,
68
-
69
- uniquId: uniquId + 1
155
+ inputValue: e.target.value,
70
156
 
71
157
  });
72
158
 
@@ -74,23 +160,19 @@
74
160
 
75
161
 
76
162
 
77
- deleteTodo() {
163
+ handleClick() {
78
-
164
+
79
- const clearTask = this.state.tasks;
165
+ const inputValue = this.state.inputValue;
80
-
166
+
81
- const deleTask = clearTask.splice(this.state.id, 1);
167
+ this.props.addTask(inputValue);
82
168
 
83
169
  this.setState({
84
170
 
85
- tasks: deleTask
171
+ inputValue: "",
86
-
172
+
87
- });
173
+ })
88
-
174
+
89
- }
175
+ }
90
-
91
-
92
-
93
-
94
176
 
95
177
 
96
178
 
@@ -100,208 +182,94 @@
100
182
 
101
183
  <div>
102
184
 
185
+ <input
186
+
187
+ type="text"
188
+
189
+ value={this.state.inputValue}
190
+
103
- <h1>Todoリスト</h1>
191
+ onChange={ this.handleChange }
104
-
192
+
105
- <TodoList tasks={this.state.tasks} deleteTodo={this.deleteTodo}/>
193
+ />
106
-
194
+
107
- <TodoAdd addTask={this.addTask}/>
195
+ <button onClick={this.handleClick}>送信</button>
108
196
 
109
197
  </div>
110
198
 
199
+ );
200
+
201
+ }
202
+
203
+ }
204
+
205
+
206
+
207
+ export default TodoAdd;
208
+
209
+ ```
210
+
211
+
212
+
213
+
214
+
215
+ export default ToDoList;
216
+
217
+ ```
218
+
219
+ class ToDoList extends React.Component{
220
+
221
+ constructor(props) {
222
+
223
+ super(props);
224
+
225
+ }
226
+
227
+
228
+
229
+ render() {
230
+
231
+ return (
232
+
233
+ <div>
234
+
235
+ <ul>
236
+
237
+ {this.props.tasks.map((item) => <li key={item.id} >
238
+
239
+ {item.text}
240
+
241
+ <button onClick={this.props.deleteTodo(item.id)}>削除</button>
242
+
243
+ </li>)}
244
+
245
+ </ul>
246
+
247
+
248
+
249
+ </div>
250
+
251
+
252
+
111
253
  );
112
254
 
113
- }
255
+ };
114
256
 
115
257
  }
116
258
 
117
-
118
-
259
+
260
+
119
- export default App;
261
+ export default TodoList;
120
-
262
+
121
- ```
263
+ ```
122
-
123
-
124
-
264
+
265
+
266
+
125
- `TodoAdd`
267
+ `index`
126
-
127
- ```
128
-
129
- import React, { Component } from 'react';
130
-
131
-
132
-
133
- class TodoAdd extends React.Component {
134
-
135
- constructor(props) {
136
-
137
- super(props);
138
-
139
- this.state = {
140
-
141
- inputValue: null,
142
-
143
- }
144
-
145
- this.handleChange = this.handleChange.bind(this);
146
-
147
- this.handleClick = this.handleClick.bind(this);
148
-
149
- }
150
-
151
-
152
-
153
- handleChange(e) {
154
-
155
- this.setState({
156
-
157
- inputValue: e.target.value,
158
-
159
- });
160
-
161
- }
162
-
163
-
164
-
165
- handleClick() {
166
-
167
- const inputValue = this.state.inputValue;
168
-
169
- this.props.addTask(inputValue);
170
-
171
- this.setState({
172
-
173
- inputValue: "",
174
-
175
- })
176
-
177
- }
178
-
179
-
180
-
181
- render() {
182
-
183
- return (
184
-
185
- <div>
186
-
187
- <input
188
-
189
- type="text"
190
-
191
- value={this.state.inputValue}
192
-
193
- onChange={ this.handleChange }
194
-
195
- />
196
-
197
- <button onClick={this.handleClick}>送信</button>
198
-
199
- </div>
200
-
201
- );
202
-
203
- }
204
-
205
- }
206
-
207
-
208
-
209
- export default TodoAdd;
210
-
211
- ```
212
-
213
- `TodoList`
214
268
 
215
269
  ```
216
270
 
217
271
  import React from 'react';
218
272
 
219
- import Todo from './Todo';
220
-
221
-
222
-
223
-
224
-
225
- const ToDoList = ({ tasks }) => (
226
-
227
-
228
-
229
- <div>
230
-
231
-
232
-
233
- {tasks.map(item =>
234
-
235
- <Todo
236
-
237
- tasks={item.tasks}
238
-
239
- text={item.text}
240
-
241
- id={item.id}
242
-
243
- deleteTodo={this.deleteTodo}
244
-
245
- />
246
-
247
- )
248
-
249
- }
250
-
251
- </div>
252
-
253
- );
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
- export default ToDoList;
262
-
263
- ```
264
-
265
-
266
-
267
- `Todo`
268
-
269
- ```
270
-
271
- class Todo extends React.Component {
272
-
273
- render() {
274
-
275
-
276
-
277
- return (
278
-
279
- <ul>
280
-
281
- <li key={this.props.id}>{this.props.text}</li>
282
-
283
- <button onClick={this.props.deleteTodo()}>削除</button>
284
-
285
- </ul>);
286
-
287
- }
288
-
289
- }
290
-
291
-
292
-
293
- export default Todo;
294
-
295
- ```
296
-
297
-
298
-
299
- `index`
300
-
301
- ```
302
-
303
- import React from 'react';
304
-
305
273
  import ReactDOM from 'react-dom';
306
274
 
307
275
  import './App.css';
@@ -336,54 +304,74 @@
336
304
 
337
305
  ```
338
306
 
339
- TypeError: Cannot read property 'deleteTodo' of undefined
307
+ Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
308
+
309
+ ▶ 4 stack frames were collapsed.
310
+
311
+ App.deleteTodo
312
+
313
+ src/App.js:35
314
+
315
+ 32 |
316
+
317
+ 33 | deleteTodo(i) {
318
+
319
+ 34 | let deleTask = this.state.tasks.splice(i, 1);
320
+
321
+ > 35 | this.setState({
322
+
323
+ 36 | tasks: deleTask
324
+
325
+ 37 | });
326
+
327
+ 38 | }
328
+
329
+ View compiled
340
330
 
341
331
  (anonymous function)
342
332
 
343
333
  src/TodoList.js:15
344
334
 
345
- 12 | tasks={item.tasks}
335
+ 12 | <ul>
336
+
346
-
337
+ 13 | {this.props.tasks.map((item) => <li key={item.id} >
338
+
347
- 13 | text={item.text}
339
+ 14 | {item.text}
348
-
340
+
349
- 14 | id={item.id}
341
+ > 15 | <button onClick={this.props.deleteTodo(item.id)}>削除</button>
350
-
342
+
351
- > 15 | deleteTodo={this.deleteTodo}
343
+ | ^ 16 | </li>)}
352
-
344
+
353
- | ^ 16 | />
345
+ 17 | </ul>
354
-
355
- 17 | )
346
+
356
-
357
- 18 | }
347
+ 18 |
358
348
 
359
349
  View compiled
360
350
 
361
- ToDoList
351
+ ToDoList.render
362
-
352
+
363
- src/TodoList.js:8
353
+ src/TodoList.js:12
364
-
354
+
365
- 5 |
355
+ 9 | render() {
366
-
356
+
367
- 6 | const ToDoList = ({ tasks }) => (
357
+ 10 | return (
368
-
369
- 7 |
358
+
370
-
371
- > 8 | <div>
359
+ 11 | <div>
372
-
373
- 9 |
360
+
374
-
375
- 10 | {tasks.map(item =>
376
-
377
- 11 | <Todo
361
+ > 12 | <ul>
362
+
378
-
363
+ | ^ 13 | {this.props.tasks.map((item) => <li key={item.id} >
364
+
379
- View compiled
365
+ 14 | {item.text}
366
+
380
-
367
+ 15 | <button onClick={this.props.deleteTodo(item.id)}>削除</button>
368
+
381
- ```
369
+ ```
382
-
383
-
384
-
385
-
386
-
370
+
371
+
372
+
373
+
374
+
387
- メソッドを定義できていない、コンポーネント間のpropsの受け渡しができていない、削除の部分が間違っているように思いますが、初心者のため、はっきりとした原因が分かりません。
375
+ 削除の部分が間違っているように思いますが、初心者のため、はっきりとした原因が分かりません。
388
376
 
389
377
  教えていただけると嬉しいです。