回答編集履歴
9
修正
test
CHANGED
@@ -314,6 +314,8 @@
|
|
314
314
|
|
315
315
|
|
316
316
|
|
317
|
+
const { children } = this.props;
|
318
|
+
|
317
319
|
// Object Destructuringで別途、デフォルト値を空配列として初期化
|
318
320
|
|
319
321
|
const { messages = []} = this.props.children;
|
@@ -322,7 +324,7 @@
|
|
322
324
|
|
323
325
|
return ([
|
324
326
|
|
325
|
-
<p key={1}>numbers: {
|
327
|
+
<p key={1}>numbers: {children.numbers.map(number => number)}</p>,
|
326
328
|
|
327
329
|
<p key={2}>messages: {messages.map(message => message)}</p>,
|
328
330
|
|
8
/
test
CHANGED
@@ -314,8 +314,6 @@
|
|
314
314
|
|
315
315
|
|
316
316
|
|
317
|
-
const children = this.props;
|
318
|
-
|
319
317
|
// Object Destructuringで別途、デフォルト値を空配列として初期化
|
320
318
|
|
321
319
|
const { messages = []} = this.props.children;
|
@@ -324,7 +322,7 @@
|
|
324
322
|
|
325
323
|
return ([
|
326
324
|
|
327
|
-
<p key={1}>numbers: {children.numbers.map(number => number)}</p>,
|
325
|
+
<p key={1}>numbers: {this.props.children.numbers.map(number => number)}</p>,
|
328
326
|
|
329
327
|
<p key={2}>messages: {messages.map(message => message)}</p>,
|
330
328
|
|
7
this.props.children -> children
test
CHANGED
@@ -324,7 +324,7 @@
|
|
324
324
|
|
325
325
|
return ([
|
326
326
|
|
327
|
-
<p key={1}>numbers: {
|
327
|
+
<p key={1}>numbers: {children.numbers.map(number => number)}</p>,
|
328
328
|
|
329
329
|
<p key={2}>messages: {messages.map(message => message)}</p>,
|
330
330
|
|
6
解決策3の追加
test
CHANGED
@@ -238,6 +238,114 @@
|
|
238
238
|
|
239
239
|
|
240
240
|
|
241
|
+
# 解決策3
|
242
|
+
|
243
|
+
Object Destructuringで使われる方のコンポーネント側で対象プロパティーのデフォルト値を設定
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
248
|
+
|
249
|
+
import React, { Component } from 'react';
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
import Sample from './Sample';
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
class App extends Component {
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
render() {
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
// messagesを空配列で初期化しないバージョン
|
266
|
+
|
267
|
+
const obj = {numbers: [1, 2, 3]};
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
return <Sample>{obj}</Sample>;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
export default App;
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
import React, { Component } from 'react';
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
class Sample extends Component {
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
static defaultProps = {
|
298
|
+
|
299
|
+
message: 'This is a single message.',
|
300
|
+
|
301
|
+
// defaultPropsでchildren.messagesの初期値を設定しても初期値として適用されない
|
302
|
+
|
303
|
+
children: {
|
304
|
+
|
305
|
+
messages: ['message1', 'message2']
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
};
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
render() {
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
const children = this.props;
|
318
|
+
|
319
|
+
// Object Destructuringで別途、デフォルト値を空配列として初期化
|
320
|
+
|
321
|
+
const { messages = []} = this.props.children;
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
return ([
|
326
|
+
|
327
|
+
<p key={1}>numbers: {this.props.children.numbers.map(number => number)}</p>,
|
328
|
+
|
329
|
+
<p key={2}>messages: {messages.map(message => message)}</p>,
|
330
|
+
|
331
|
+
<p key={3}>message: {this.props.message}</p>
|
332
|
+
|
333
|
+
])
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
export default Sample;
|
344
|
+
|
345
|
+
```
|
346
|
+
|
347
|
+
|
348
|
+
|
241
349
|
## 出力イメージ
|
242
350
|
|
243
351
|
|
@@ -245,3 +353,9 @@
|
|
245
353
|

|
246
354
|
|
247
355
|
両方とも結果は同じです。
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
## 更新
|
360
|
+
|
361
|
+
解決策3を追記しました。
|
5
回答の前提
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
# 前提
|
2
|
+
|
1
|
-
Reactの利用を考慮して、それをふまえて何が出来そうか検討してみました。
|
3
|
+
(1).Reactの利用を考慮して、それをふまえて何が出来そうか検討してみました。
|
4
|
+
|
5
|
+
(2).`children['msg_list']`から、親コンポーネントのchildrenは単一であることを回答の前提としました。
|
2
6
|
|
3
7
|
|
4
8
|
|
4
shallow copy
test
CHANGED
@@ -190,6 +190,8 @@
|
|
190
190
|
|
191
191
|
|
192
192
|
|
193
|
+
// shallow copyなのでchildrenのネストが深い場合は要注意
|
194
|
+
|
193
195
|
const children = Object.entries(
|
194
196
|
|
195
197
|
this.props.children
|
3
太字
test
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
6
|
-
|
7
5
|
# 解決策1
|
8
6
|
|
9
7
|
|
10
8
|
|
11
9
|
コンポーネントを使う側のコンポーネントで対象プロパティーに空配列を初期値として渡してあげる。
|
12
10
|
|
11
|
+
|
12
|
+
|
13
|
-
ちなみに、使われる側のコンポーネントのdefaultPropsでchildrenの初期値を設定しようと試みたところ、childrenの初期値は設定不可のようでした。
|
13
|
+
**ちなみに、使われる側のコンポーネントのdefaultPropsでchildrenの初期値を設定しようと試みたところ、childrenの初期値は設定不可のようでした。**
|
14
14
|
|
15
15
|
|
16
16
|
|
2
spell
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
コンポーネントを使う側のコンポーネントで対象プロパティーに空配列を初期値として渡してあげる。
|
12
12
|
|
13
|
-
ちなみに、使われる側のコンポーネントのdefaultPropsでchildrenの初期値を設定しようと試みたところ、childreの初期値は設定不可のようでした。
|
13
|
+
ちなみに、使われる側のコンポーネントのdefaultPropsでchildrenの初期値を設定しようと試みたところ、childrenの初期値は設定不可のようでした。
|
14
14
|
|
15
15
|
|
16
16
|
|
1
画像追加
test
CHANGED
@@ -229,3 +229,13 @@
|
|
229
229
|
export default Sample;
|
230
230
|
|
231
231
|
```
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
## 出力イメージ
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+

|
240
|
+
|
241
|
+
両方とも結果は同じです。
|