質問編集履歴

2

修正

2018/03/14 10:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -278,7 +278,7 @@
278
278
 
279
279
  <div>
280
280
 
281
- <button onClick={() => {this.buttonClick();}>ボタン</button>
281
+ <button onClick={() => {this.buttonClick();}}>ボタン</button>
282
282
 
283
283
  </div>
284
284
 

1

コードの追加

2018/03/14 10:03

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 子のcomponent親のstateが変わた際にpropsで渡したい
1
+ 子のcomponentから親のstateを更新させた後に子てるpropsを更新させたい
test CHANGED
@@ -1,3 +1,297 @@
1
1
  あるcomponentでcomponentWillUpdate()をし、
2
2
 
3
3
  state、またはpropsが更新されたタイミングで子のcomponentにpropsで親のcomponentのstateを渡したいのですがどうすればできますか?
4
+
5
+
6
+
7
+ ## コード
8
+
9
+ 親component
10
+
11
+ ```
12
+
13
+ import React from 'react';
14
+
15
+ import PropTypes from 'prop-types';
16
+
17
+ import component1 from './component1';
18
+
19
+ import component2 from './component2';
20
+
21
+
22
+
23
+
24
+
25
+ class parent extends React.Component {
26
+
27
+ constructor(props) {
28
+
29
+ super(props);
30
+
31
+
32
+
33
+ this.state = {
34
+
35
+ array: [
36
+
37
+ { test: 'test1', test1: false, index: 0},
38
+
39
+ { test: 'test2', test1: false, index: 1},
40
+
41
+ { test: 'test3', test1: false, index: 2},
42
+
43
+ ],
44
+
45
+ };
46
+
47
+ }
48
+
49
+
50
+
51
+ onSubmitState() {
52
+
53
+ this.setState({
54
+
55
+ array: [
56
+
57
+ { test: 'aaa', test1: true, index: 0},
58
+
59
+ { test: 'bbb', test1: true, index: 1},
60
+
61
+ { test: 'ccc', test1: true, index: 2},
62
+
63
+ ],
64
+
65
+ });
66
+
67
+ }
68
+
69
+
70
+
71
+ cmp1Props(obj) {
72
+
73
+ return (
74
+
75
+ <component1
76
+
77
+ cmp1Props={obj}
78
+
79
+ />
80
+
81
+ );
82
+
83
+ }
84
+
85
+
86
+
87
+ cmp2Props(obj) {
88
+
89
+ return (
90
+
91
+ <component2
92
+
93
+ cmp2Props={obj},
94
+
95
+ onSubmitState={() => {this.onSubmitState();}}
96
+
97
+ />
98
+
99
+ );
100
+
101
+ }
102
+
103
+ render() {
104
+
105
+ const cmp1 = this.state.array.map((e) => {
106
+
107
+ return (
108
+
109
+ this.cmp1Props(e)
110
+
111
+ );
112
+
113
+ });
114
+
115
+
116
+
117
+ const cmp2 = this.state.array.map((e) => {
118
+
119
+ return (
120
+
121
+ this.cmp2Props(e)
122
+
123
+ );
124
+
125
+ });
126
+
127
+
128
+
129
+ return (
130
+
131
+ <div>
132
+
133
+ {cmp1}
134
+
135
+ {cmp2}
136
+
137
+ </div>
138
+
139
+ );
140
+
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ export default parent;
148
+
149
+ ```
150
+
151
+
152
+
153
+ component1(子)
154
+
155
+ ```
156
+
157
+ import React from 'react';
158
+
159
+ import PropTypes from 'prop-types';
160
+
161
+
162
+
163
+ class component1 extends React.Component {
164
+
165
+ static propTypes = {
166
+
167
+ cmp1Props: PropTypes.object,
168
+
169
+ };
170
+
171
+
172
+
173
+ constructor(props) {
174
+
175
+ super(props);
176
+
177
+
178
+
179
+ this.state = {
180
+
181
+ test: this.props.cmp1Props.test,
182
+
183
+ test1: this.props.cmp1Props.test1,
184
+
185
+ };
186
+
187
+ }
188
+
189
+
190
+
191
+ render() {
192
+
193
+ if(this.state.test1) {
194
+
195
+ return(
196
+
197
+ <div>
198
+
199
+ <p>成功</p>
200
+
201
+ </div>
202
+
203
+ );
204
+
205
+ } else {
206
+
207
+ return(
208
+
209
+ <div>
210
+
211
+ <p>失敗</p>
212
+
213
+ </div>
214
+
215
+ );
216
+
217
+ }
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+ export default component1;
226
+
227
+ ```
228
+
229
+
230
+
231
+ component2(子)
232
+
233
+ ```
234
+
235
+ import React from 'react';
236
+
237
+ import PropTypes from 'prop-types';
238
+
239
+
240
+
241
+ const propTypes = {
242
+
243
+ cmp2Props: PropTypes.object,
244
+
245
+ onSubmitState: PropTypes.func
246
+
247
+ };
248
+
249
+
250
+
251
+ class component2 extends React.Component {
252
+
253
+ constructor(props) {
254
+
255
+ super(props);
256
+
257
+ this.state = {
258
+
259
+ index: this.props.cmp2Props.index,
260
+
261
+ };
262
+
263
+ }
264
+
265
+
266
+
267
+ buttonClick() {
268
+
269
+ return this.props.onSubmitState();
270
+
271
+ }
272
+
273
+
274
+
275
+ render() {
276
+
277
+ return(
278
+
279
+ <div>
280
+
281
+ <button onClick={() => {this.buttonClick();}>ボタン</button>
282
+
283
+ </div>
284
+
285
+ );
286
+
287
+ }
288
+
289
+ }
290
+
291
+
292
+
293
+ component2.propTypes = propTypes;
294
+
295
+ export default component2;
296
+
297
+ ```