質問編集履歴

1

内容編集

2020/08/06 06:48

投稿

tomaa
tomaa

スコア84

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,254 @@
22
22
 
23
23
 
24
24
 
25
+ ### 追記(最初の回答をいただいた後)
26
+
27
+
28
+
29
+ 最初の回答を頂き下記のようにコードを変更しました。
30
+
31
+ コンポーネントの追加と、通常どおりの連続した入力ができるようになりました。
32
+
33
+
34
+
35
+ しかし、TextInputで入力したテキストがStateに保管されていない状態となってしまっています。
36
+
37
+ 子コンポーネントのコード内の`//①コンソールチェック`の出力は、`Object {}`となってしまします。
38
+
39
+
40
+
41
+ ちなみに、子コンポーネントで入力した値と、親コンポーネントで利用したいため、子コンポーネントのStateの値は親からpropsで渡しています。
42
+
43
+
44
+
45
+ 親コンポーネント
46
+
47
+ ```JS
48
+
49
+ import React, { useState, useEffect } from "react";
50
+
51
+ import { View, StyleSheet } from "react-native";
52
+
53
+ import TestListInputItem from "../components/TestListInputItem";
54
+
55
+
56
+
57
+ export default function TestListScreen() {
58
+
59
+ const [texts, setTexts] = useState({});
60
+
61
+
62
+
63
+ return (
64
+
65
+ <View style={styles.container}>
66
+
67
+ <TestListInputItem texts={texts} setTexts={setTexts} />
68
+
69
+ </View>
70
+
71
+ );
72
+
73
+ }
74
+
75
+
76
+
77
+ const styles = StyleSheet.create({
78
+
79
+ container: {
80
+
81
+ flex: 1,
82
+
83
+ justifyContent: "center",
84
+
85
+ },
86
+
87
+ });
88
+
89
+ ```
90
+
91
+
92
+
93
+
94
+
95
+ 子コンポーネント
96
+
97
+
98
+
99
+ ```JS
100
+
101
+ import React, { useState, useEffect } from "react";
102
+
103
+ import {
104
+
105
+ View,
106
+
107
+ StyleSheet,
108
+
109
+ Text,
110
+
111
+ TouchableOpacity,
112
+
113
+ TextInput,
114
+
115
+ } from "react-native";
116
+
117
+
118
+
119
+ function Item({ setText, handleMakeNew, i }) {
120
+
121
+ return (
122
+
123
+ <View style={styles.list}>
124
+
125
+ <Text>{i}</Text>
126
+
127
+ <TextInput
128
+
129
+ style={styles.InputName}
130
+
131
+ onChange={(e) => {
132
+
133
+ setText(i, e.target.value);
134
+
135
+ }}
136
+
137
+ ></TextInput>
138
+
139
+ <TouchableOpacity
140
+
141
+ onPress={() => {
142
+
143
+ handleMakeNew();
144
+
145
+ }}
146
+
147
+ style={styles.buttonAdd}
148
+
149
+ >
150
+
151
+ <Text>追加する</Text>
152
+
153
+ </TouchableOpacity>
154
+
155
+ </View>
156
+
157
+ );
158
+
159
+ }
160
+
161
+
162
+
163
+ export default function TestListInputItem(props) {
164
+
165
+ const [count, setCount] = useState(1);
166
+
167
+ const [texts] = useState(props.texts);
168
+
169
+
170
+
171
+ function handleMakeNew() {
172
+
173
+ setCount((v) => v + 1);
174
+
175
+ console.log(texts);//①コンソールチェック
176
+
177
+ }
178
+
179
+ return (
180
+
181
+ <View>
182
+
183
+ <Text>{JSON.stringify(texts)}</Text>
184
+
185
+ {[...Array(count).keys()].map((i) => (
186
+
187
+ <Item
188
+
189
+ handleMakeNew={handleMakeNew}
190
+
191
+ setText={(i, text) =>
192
+
193
+ props.setTexts((prev) => ({ ...prev, [i]: text }))
194
+
195
+ }
196
+
197
+ key={i}
198
+
199
+ i={i}
200
+
201
+ />
202
+
203
+ ))}
204
+
205
+ </View>
206
+
207
+ );
208
+
209
+ }
210
+
211
+
212
+
213
+ const styles = StyleSheet.create({
214
+
215
+ list: {
216
+
217
+ width: "100%",
218
+
219
+ backgroundColor: "#ddd",
220
+
221
+ padding: 10,
222
+
223
+ },
224
+
225
+ InputName: {
226
+
227
+ borderWidth: 1,
228
+
229
+ height: 40,
230
+
231
+ backgroundColor: "#fff",
232
+
233
+ },
234
+
235
+ buttonAdd: {
236
+
237
+ backgroundColor: "orange",
238
+
239
+ width: 80,
240
+
241
+ height: 40,
242
+
243
+ margin: 3,
244
+
245
+ justifyContent: "center",
246
+
247
+ alignItems: "center",
248
+
249
+ alignSelf: "center",
250
+
251
+ },
252
+
253
+ });
254
+
255
+ ```
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+ 追記終了
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
25
273
 
26
274
 
27
275
  ### 該当のソースコード