回答編集履歴

12

2018/03/26 02:51

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -1,4 +1,4 @@
1
- ※当初の質問から大きく内容に変更があったため、回答を一旦、全削除しま
1
+ **※当初の質問から大きく内容に変更があったため、回答を一旦、全削除しました**
2
2
 
3
3
 
4
4
 
@@ -259,3 +259,25 @@
259
259
 
260
260
 
261
261
  その上でも尚、得たい結果に描画の時点でならない場合は、(A)reducerに期待しているデータが渡ってきていない、(B)描画する側でミスってる、のいずれかのバグが更にあるのだと思います。
262
+
263
+
264
+
265
+ > Reducerのロジックについて全然理解していないのですが、
266
+
267
+ &
268
+
269
+ > reducerについてよく理解していないので、これが合っているのか間違っているのかもわかりません。
270
+
271
+
272
+
273
+ であれば、まずはそこを理解するように努めてから先へ進んだ方が効率も良いのではないでしょうか。
274
+
275
+
276
+
277
+ 2段飛ばし、3段飛ばしの状態で進んでいても非効率かと思いますし、また、その状態で質問をしても、
278
+
279
+ 的はずれな質問になってしまう可能性が高くなったり、「動くコードに直してくれ」レベルの質問になってしまったり、
280
+
281
+ 回答を得られたとしても、2段飛ばし、3段飛ばしになっている状態に合わせた回答が寄せられたりして、
282
+
283
+ せっかく得られた回答も理解できずに無駄になってしまったりするのかなと思います。

11

追記

2018/03/26 02:51

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -227,3 +227,35 @@
227
227
 
228
228
 
229
229
  APIから新規追加したデータのみ返してもらえるならば、reducerのactionにはその新規データのみが渡ってくるので、それを既存のリストに追加してあげれば良いだけの話かと思います。
230
+
231
+
232
+
233
+ # 追記
234
+
235
+ > しかし、ここでただ単に追加するだけなのであれば、以下のコードで実現します。
236
+
237
+ しかし、ここでは
238
+
239
+ ボタン押す→サーバーに追加→サーバーから全データを取得→描画
240
+
241
+ としたいので、以下のコードでは問題が起こります。
242
+
243
+
244
+
245
+ いや、「ボタン押す→サーバーに追加→サーバーから全データを取得→描画」に関係なく、
246
+
247
+ 単に追加するだけであっても掲載のコードでは実現しないですよね?
248
+
249
+
250
+
251
+ 2次元配列のリストがreducerのactionに渡ってくるということなのですから、それが掲載されているreducerのコードの引数になったとして、得たい結果にはならないはずです。また、これが二次元配列ではなく編集前の配列の中にオブジェクトがあるような場合であっても、掲載されているコードでは得たい結果は得られていないはずです。
252
+
253
+
254
+
255
+ なので、まずは「ボタン押す→サーバーに追加→サーバーから全データを取得→描画」のことをreducerから切り離して、
256
+
257
+ reducer単体でしっかりと得たい結果が得られるコードにしましょう。これだけで他にバグがないのであれば得たい結果に自然となるはずです。
258
+
259
+
260
+
261
+ その上でも尚、得たい結果に描画の時点でならない場合は、(A)reducerに期待しているデータが渡ってきていない、(B)描画する側でミスってる、のいずれかのバグが更にあるのだと思います。

10

a

2018/03/26 02:43

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -98,15 +98,15 @@
98
98
 
99
99
  questionList: [
100
100
 
101
- { title: 'title1', message: 'message1' },
101
+ { title: 'title1', message: 'message1' },
102
-
102
+
103
- { title: 'title2', message: 'message2' },
103
+ { title: 'title2', message: 'message2' },
104
-
104
+
105
- { title: 'title3', message: 'message3' },
105
+ { title: 'title3', message: 'message3' },
106
-
106
+
107
- { title: 'title4', message: 'message4' },
107
+ { title: 'title4', message: 'message4' },
108
-
108
+
109
- { title: 'title5', message: 'message5' }
109
+ { title: 'title5', message: 'message5' }
110
110
 
111
111
  ]
112
112
 
@@ -118,17 +118,17 @@
118
118
 
119
119
  questionList: [
120
120
 
121
- { title: 'title1', message: 'message1' },
121
+ { title: 'title1', message: 'message1' },
122
-
122
+
123
- { title: 'title2', message: 'message2' },
123
+ { title: 'title2', message: 'message2' },
124
-
124
+
125
- { title: 'title3', message: 'message3' },
125
+ { title: 'title3', message: 'message3' },
126
-
126
+
127
- { title: 'title4', message: 'message4' },
127
+ { title: 'title4', message: 'message4' },
128
-
128
+
129
- { title: 'title5', message: 'message5' },
129
+ { title: 'title5', message: 'message5' },
130
-
130
+
131
- { title: 'title6', message: 'message6' }
131
+ { title: 'title6', message: 'message6' }
132
132
 
133
133
  ]
134
134
 
@@ -148,17 +148,17 @@
148
148
 
149
149
  payload: [
150
150
 
151
- { title: 'title1', message: 'message1' },
151
+ { title: 'title1', message: 'message1' },
152
-
152
+
153
- { title: 'title2', message: 'message2' },
153
+ { title: 'title2', message: 'message2' },
154
-
154
+
155
- { title: 'title3', message: 'message3' },
155
+ { title: 'title3', message: 'message3' },
156
-
156
+
157
- { title: 'title4', message: 'message4' },
157
+ { title: 'title4', message: 'message4' },
158
-
158
+
159
- { title: 'title5', message: 'message5' },
159
+ { title: 'title5', message: 'message5' },
160
-
160
+
161
- { title: 'title6', message: 'message6' }
161
+ { title: 'title6', message: 'message6' }
162
162
 
163
163
  ]
164
164
 
@@ -192,7 +192,7 @@
192
192
 
193
193
  payload: [
194
194
 
195
- { title: 'title6', message: 'message6' }
195
+ { title: 'title6', message: 'message6' }
196
196
 
197
197
  ]
198
198
 

9

ライブラリ無しでディープコピーは面倒なので、サンプルコードでは配列ではなくオブジェクトに変更します。。。

2018/03/25 13:29

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -92,19 +92,21 @@
92
92
 
93
93
 
94
94
 
95
+ // ディープコピーは面倒なので、サンプルコードでは配列ではなくオブジェクトに変更します。。。
96
+
95
97
  const initialState = {
96
98
 
97
99
  questionList: [
98
100
 
99
- ['title1', 'message1'],
101
+ { title: 'title1', message: 'message1' },
100
-
102
+
101
- ['title2', 'message2'],
103
+ { title: 'title2', message: 'message2' },
102
-
104
+
103
- ['title3', 'message3'],
105
+ { title: 'title3', message: 'message3' },
104
-
106
+
105
- ['title4', 'message4'],
107
+ { title: 'title4', message: 'message4' },
106
-
108
+
107
- ['title5', 'message5']
109
+ { title: 'title5', message: 'message5' }
108
110
 
109
111
  ]
110
112
 
@@ -116,17 +118,17 @@
116
118
 
117
119
  questionList: [
118
120
 
119
- ['title1', 'message1'],
121
+ { title: 'title1', message: 'message1' },
120
-
122
+
121
- ['title2', 'message2'],
123
+ { title: 'title2', message: 'message2' },
122
-
124
+
123
- ['title3', 'message3'],
125
+ { title: 'title3', message: 'message3' },
124
-
126
+
125
- ['title4', 'message4'],
127
+ { title: 'title4', message: 'message4' },
126
-
128
+
127
- ['title5', 'message5'],
129
+ { title: 'title5', message: 'message5' },
128
-
130
+
129
- ['title6', 'message6'],
131
+ { title: 'title6', message: 'message6' }
130
132
 
131
133
  ]
132
134
 
@@ -146,17 +148,17 @@
146
148
 
147
149
  payload: [
148
150
 
149
- ['title1', 'message1'],
151
+ { title: 'title1', message: 'message1' },
150
-
152
+
151
- ['title2', 'message2'],
153
+ { title: 'title2', message: 'message2' },
152
-
154
+
153
- ['title3', 'message3'],
155
+ { title: 'title3', message: 'message3' },
154
-
156
+
155
- ['title4', 'message4'],
157
+ { title: 'title4', message: 'message4' },
156
-
158
+
157
- ['title5', 'message5'],
159
+ { title: 'title5', message: 'message5' },
158
-
160
+
159
- ['title6', 'message6']
161
+ { title: 'title6', message: 'message6' }
160
162
 
161
163
  ]
162
164
 
@@ -190,7 +192,7 @@
190
192
 
191
193
  payload: [
192
194
 
193
- ['title6', 'message6']
195
+ { title: 'title6', message: 'message6' }
194
196
 
195
197
  ]
196
198
 

8

a

2018/03/25 13:27

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -28,7 +28,9 @@
28
28
 
29
29
 
30
30
 
31
- 例えば、掲載されている内容を愚直にreducerのコードテストコード付きでとし込むのであれば以下なります。
31
+ 例えば、掲載されている内容を該当reducerのコードテストコード付きで書くとしたら以下のようになります。
32
+
33
+
32
34
 
33
35
 
34
36
 
@@ -38,13 +40,11 @@
38
40
 
39
41
  export default function questionListsReducer(state, action) {
40
42
 
41
-
42
-
43
- switch(action.type) {
43
+ switch (action.type) {
44
+
44
-
45
+ //質問文に記載通りのかたち
45
-
46
-
46
+
47
- case 'update_all':
47
+ case "update_all":
48
48
 
49
49
  return {
50
50
 
@@ -54,16 +54,28 @@
54
54
 
55
55
 
56
56
 
57
+ //新規追加データのみreducerに渡ってきた場合
58
+
59
+ case "add_questions":
60
+
61
+ return {
62
+
63
+ questionList: [...state.questionList, ...action.payload]
64
+
65
+ };
66
+
67
+
68
+
57
69
  default:
58
70
 
59
71
  return state;
60
72
 
61
73
  }
62
74
 
63
-
64
-
65
75
  }
66
76
 
77
+
78
+
67
79
  ```
68
80
 
69
81
 
@@ -100,13 +112,9 @@
100
112
 
101
113
 
102
114
 
103
- const action = {
115
+ const expectedResult = {
104
-
105
- type: 'update_all',
116
+
106
-
107
- // 質問文に書かれていたように全データが渡ってくるとする
108
-
109
- payload: [
117
+ questionList: [
110
118
 
111
119
  ['title1', 'message1'],
112
120
 
@@ -118,32 +126,10 @@
118
126
 
119
127
  ['title5', 'message5'],
120
128
 
121
- ['title6', 'message6']
129
+ ['title6', 'message6'],
122
130
 
123
131
  ]
124
132
 
125
- }
126
-
127
-
128
-
129
- const expectedResult = {
130
-
131
- questionList:[
132
-
133
- ['title1', 'message1'],
134
-
135
- ['title2', 'message2'],
136
-
137
- ['title3', 'message3'],
138
-
139
- ['title4', 'message4'],
140
-
141
- ['title5', 'message5'],
142
-
143
- ['title6', 'message6'],
144
-
145
- ]
146
-
147
133
  };
148
134
 
149
135
 
@@ -152,18 +138,78 @@
152
138
 
153
139
 
154
140
 
141
+ const action = {
142
+
143
+ type: 'update_all',
144
+
145
+ // 質問文に書かれていたように全データが渡ってくるとする
146
+
147
+ payload: [
148
+
149
+ ['title1', 'message1'],
150
+
151
+ ['title2', 'message2'],
152
+
153
+ ['title3', 'message3'],
154
+
155
+ ['title4', 'message4'],
156
+
157
+ ['title5', 'message5'],
158
+
159
+ ['title6', 'message6']
160
+
161
+ ]
162
+
163
+ }
164
+
165
+
166
+
155
167
  const result = questionListsReducer(initialState, action);
156
168
 
157
169
 
158
170
 
159
171
  expect(result).toEqual(expectedResult); //test passes.
160
172
 
161
-
173
+
162
174
 
163
175
  });
164
176
 
165
177
 
166
178
 
179
+
180
+
181
+ it('should add new questions to the existing questionList', () => {
182
+
183
+
184
+
185
+ const action = {
186
+
187
+ type: 'add_questions',
188
+
189
+ // 追加した新規データのみ渡ってくるとする
190
+
191
+ payload: [
192
+
193
+ ['title6', 'message6']
194
+
195
+ ]
196
+
197
+ }
198
+
199
+
200
+
201
+ const result = questionListsReducer(initialState, action);
202
+
203
+
204
+
205
+ expect(result).toEqual(expectedResult); //test passes.
206
+
207
+
208
+
209
+ });
210
+
211
+
212
+
167
213
  })
168
214
 
169
215
  ```
@@ -175,3 +221,7 @@
175
221
 
176
222
 
177
223
  新規追加したデータだけAPIサーバーから返してもらうことができるなら、そうした方が良いかと思いますが、全データを取得することが必須なのであれば、単純に上記コードのように、既存のstate内のリストをAPIから取得した全リストに置き換えてしまえば良いだけかと思います。
224
+
225
+
226
+
227
+ APIから新規追加したデータのみ返してもらえるならば、reducerのactionにはその新規データのみが渡ってくるので、それを既存のリストに追加してあげれば良いだけの話かと思います。

7

a

2018/03/25 13:16

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -104,6 +104,8 @@
104
104
 
105
105
  type: 'update_all',
106
106
 
107
+ // 質問文に書かれていたように全データが渡ってくるとする
108
+
107
109
  payload: [
108
110
 
109
111
  ['title1', 'message1'],

6

2018/03/25 12:43

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -1 +1,175 @@
1
1
  ※当初の質問から大きく内容に変更があったため、回答を一旦、全削除します。
2
+
3
+
4
+
5
+ > しかし、ここでただ単に追加するだけなのであれば、以下のコードで実現します。
6
+
7
+ しかし、ここでは
8
+
9
+ ボタン押す→サーバーに追加→サーバーから全データを取得→描画
10
+
11
+ としたいので、以下のコードでは問題が起こります。
12
+
13
+
14
+
15
+ いえ、関係ないです。
16
+
17
+
18
+
19
+ 質問を追加するボタンのことや、API、データベースのことは一旦忘れてください。
20
+
21
+
22
+
23
+ reducerは現在のstateとactionオブジェクトを引数にとって、新しいstateを返す、または現在のstateをそのまま返す関数にしか過ぎません。
24
+
25
+
26
+
27
+ まず、reducer単体で期待する結果を得られるようにコードを書いてください。
28
+
29
+
30
+
31
+ 例えば、掲載されている内容を愚直にreducerのコードにテストコード付きで落とし込むのであれば以下となります。
32
+
33
+
34
+
35
+ // questionListsReducer.js
36
+
37
+ ```
38
+
39
+ export default function questionListsReducer(state, action) {
40
+
41
+
42
+
43
+ switch(action.type) {
44
+
45
+
46
+
47
+ case 'update_all':
48
+
49
+ return {
50
+
51
+ questionList: action.payload
52
+
53
+ };
54
+
55
+
56
+
57
+ default:
58
+
59
+ return state;
60
+
61
+ }
62
+
63
+
64
+
65
+ }
66
+
67
+ ```
68
+
69
+
70
+
71
+ // questionListsReducer.test.js
72
+
73
+ ```
74
+
75
+ import questionListsReducer from './questionListsReducer';
76
+
77
+
78
+
79
+ describe('questionListsReducer', () => {
80
+
81
+
82
+
83
+ const initialState = {
84
+
85
+ questionList: [
86
+
87
+ ['title1', 'message1'],
88
+
89
+ ['title2', 'message2'],
90
+
91
+ ['title3', 'message3'],
92
+
93
+ ['title4', 'message4'],
94
+
95
+ ['title5', 'message5']
96
+
97
+ ]
98
+
99
+ }
100
+
101
+
102
+
103
+ const action = {
104
+
105
+ type: 'update_all',
106
+
107
+ payload: [
108
+
109
+ ['title1', 'message1'],
110
+
111
+ ['title2', 'message2'],
112
+
113
+ ['title3', 'message3'],
114
+
115
+ ['title4', 'message4'],
116
+
117
+ ['title5', 'message5'],
118
+
119
+ ['title6', 'message6']
120
+
121
+ ]
122
+
123
+ }
124
+
125
+
126
+
127
+ const expectedResult = {
128
+
129
+ questionList:[
130
+
131
+ ['title1', 'message1'],
132
+
133
+ ['title2', 'message2'],
134
+
135
+ ['title3', 'message3'],
136
+
137
+ ['title4', 'message4'],
138
+
139
+ ['title5', 'message5'],
140
+
141
+ ['title6', 'message6'],
142
+
143
+ ]
144
+
145
+ };
146
+
147
+
148
+
149
+ it('should replace an existing questionList with a new questionList', () => {
150
+
151
+
152
+
153
+ const result = questionListsReducer(initialState, action);
154
+
155
+
156
+
157
+ expect(result).toEqual(expectedResult); //test passes.
158
+
159
+
160
+
161
+ });
162
+
163
+
164
+
165
+ })
166
+
167
+ ```
168
+
169
+
170
+
171
+ > サーバーから全データを取得
172
+
173
+
174
+
175
+ 新規追加したデータだけAPIサーバーから返してもらうことができるなら、そうした方が良いかと思いますが、全データを取得することが必須なのであれば、単純に上記コードのように、既存のstate内のリストをAPIから取得した全リストに置き換えてしまえば良いだけかと思います。

5

アプデ

2018/03/25 12:37

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -1,207 +1 @@
1
- ごめんなさい。何を質問されたいのかよく分からなかったです。
2
-
3
-
4
-
5
- questionListsReducerに渡ってくるactionの中身と、それを踏まえて、どういうstateを返したいのかを追記すると、質問内容がわかりやすくなるかと思います。(dbやviewに関する情報は、reducerの話をする上で不要な情報になると思われます。)
6
-
7
-
8
-
9
-
10
-
11
- それよりも、以下のように変更してはダメなのですか?
12
-
13
-
14
-
15
-
16
-
17
- #1
18
-
19
-
20
-
21
- ```
22
-
23
- /*
24
-
25
- * InitialState
26
-
27
- ===================================*/
28
-
29
- const initialStateQuestionList = {
30
-
31
- title: null,
32
-
33
- message: null,
34
-
35
- }
36
-
37
-
38
-
39
- /*
40
-
41
- * questionListReducer
42
-
43
- ===================================*/
44
-
45
- export const questionListReducer = (state = initialStateQuestionList, action) => {
46
-
47
- switch (action.type) {
48
-
49
- /*= QuestionPage =========*/
50
-
51
- case actionTypes.SET_QUESTION_TITLE:
1
+ ※当初の質問から大きく内容に変更があったため、回答を一旦、全削除します。
52
-
53
- if (state.id !== action.id) {
54
-
55
- return state
56
-
57
- }
58
-
59
- return Object.assign({}, state, {
60
-
61
- title: action.title,
62
-
63
- message: action.message,
64
-
65
- })
66
-
67
-
68
-
69
- default:
70
-
71
- return state
72
-
73
- }
74
-
75
- }
76
-
77
-
78
-
79
- /*
80
-
81
- * questionListsReducer
82
-
83
- ===================================*/
84
-
85
- const initialState = {
86
-
87
- questionList: [],
88
-
89
- }
90
-
91
-
92
-
93
- ```
94
-
95
-
96
-
97
-
98
-
99
- これは全部削除
100
-
101
-
102
-
103
-
104
-
105
- #2
106
-
107
- ```
108
-
109
- /*
110
-
111
- * questionListsReducer
112
-
113
- ===================================*/
114
-
115
- const initialState = {
116
-
117
- questionList: [],
118
-
119
- }
120
-
121
-
122
-
123
- ```
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
- ```
132
-
133
- const initialState = [];
134
-
135
- ```
136
-
137
-
138
-
139
- `questionLists`というより、`questionList`または、`questions`なのではないですか?
140
-
141
-
142
-
143
- ```
144
-
145
- const initialStateQuestionList = {
146
-
147
- title: null,
148
-
149
- message: null,
150
-
151
- }
152
-
153
- ```
154
-
155
- ↑ここは、xxxListと命名されていますが、Listでもなんでもなく、単に1つのquestionですよね?
156
-
157
-
158
-
159
- なので、`const initialState = [];`で、この中に個々のquestionが入ればよいのでは?
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
- #3
168
-
169
- ```
170
-
171
- case actionTypes.SET_QUESTION_TITLE:
172
-
173
- return Object.assign({}, state, {
174
-
175
- questionList: state.questionList.map(t =>
176
-
177
- questionListReducer(t, action)
178
-
179
- )
180
-
181
- })
182
-
183
- ```
184
-
185
-
186
-
187
- ここでやりたいことは、新しいquestionをquestionListに追加することですか?
188
-
189
-
190
-
191
- それであれば、initialStateを`[]`に前述の通り変更することで、あとは、stateをコピーして、新しいquestionをその配列に加えて、新しいstateを返すだけの話ですよ。なので、`questionListReducer`は不要では?
192
-
193
-
194
-
195
- > Reducerのロジックについて全然理解していないのですが、
196
-
197
-
198
-
199
- &
200
-
201
-
202
-
203
- > reducerについてよく理解していないので、これが合っているのか間違っているのかもわかりません。
204
-
205
-
206
-
207
- であれば、まずはそこを理解するように努めてから先へ進んだ方が効率も良いのではないでしょうか。

4

a

2018/03/25 12:26

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
 
4
4
 
5
+ questionListsReducerに渡ってくるactionの中身と、それを踏まえて、どういうstateを返したいのかを追記すると、質問内容がわかりやすくなるかと思います。(dbやviewに関する情報は、reducerの話をする上で不要な情報になると思われます。)
6
+
7
+
8
+
9
+
10
+
5
11
  それよりも、以下のように変更してはダメなのですか?
6
12
 
7
13
 

3

2018/03/25 09:50

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -190,4 +190,12 @@
190
190
 
191
191
 
192
192
 
193
+ &
194
+
195
+
196
+
197
+ > reducerについてよく理解していないので、これが合っているのか間違っているのかもわかりません。
198
+
199
+
200
+
193
201
  であれば、まずはそこを理解するように努めてから先へ進んだ方が効率も良いのではないでしょうか。

2

2018/03/25 09:11

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -183,3 +183,11 @@
183
183
 
184
184
 
185
185
  それであれば、initialStateを`[]`に前述の通り変更することで、あとは、stateをコピーして、新しいquestionをその配列に加えて、新しいstateを返すだけの話ですよ。なので、`questionListReducer`は不要では?
186
+
187
+
188
+
189
+ > Reducerのロジックについて全然理解していないのですが、
190
+
191
+
192
+
193
+ であれば、まずはそこを理解するように努めてから先へ進んだ方が効率も良いのではないでしょうか。

1

2018/03/25 09:06

投稿

HayatoKamono
HayatoKamono

スコア2415

test CHANGED
@@ -182,4 +182,4 @@
182
182
 
183
183
 
184
184
 
185
- それであれば、initialStateを`[]`に前述の通り変更することで、あとは、stateをコピーして、新しいquestionをその配列に加えて、新しいstateを返すだけの話ですよ。
185
+ それであれば、initialStateを`[]`に前述の通り変更することで、あとは、stateをコピーして、新しいquestionをその配列に加えて、新しいstateを返すだけの話ですよ。なので、`questionListReducer`は不要では?