質問編集履歴
1
タイトルの変更、 本文の一部削除
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
redux-form
|
1
|
+
redux-form actionの初期値を表示する
|
test
CHANGED
@@ -22,27 +22,27 @@
|
|
22
22
|
|
23
23
|
```ここに言語を入力
|
24
24
|
|
25
|
-
export function a
|
25
|
+
export function handleChange(text){
|
26
26
|
|
27
27
|
return{
|
28
28
|
|
29
|
-
type: '
|
29
|
+
type: 'TEXT_CHANGED',
|
30
|
-
|
30
|
+
|
31
|
-
|
31
|
+
text: text
|
32
|
-
|
33
|
-
|
32
|
+
|
34
|
-
|
35
|
-
}
|
33
|
+
}
|
34
|
+
|
36
|
-
|
35
|
+
}
|
37
|
-
|
38
|
-
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
-
export function
|
39
|
+
export function addRecipe(newrecipe){
|
40
40
|
|
41
41
|
return{
|
42
42
|
|
43
|
-
type: '
|
43
|
+
type: 'RECIPE_ADDED',
|
44
|
-
|
44
|
+
|
45
|
-
|
45
|
+
payload: newrecipe
|
46
46
|
|
47
47
|
}
|
48
48
|
|
@@ -50,374 +50,330 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
+
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
AddRecipes.js/reducers
|
60
|
+
|
61
|
+
```ここに言語を入力
|
62
|
+
|
63
|
+
const AddRecipes = (state = initialState, action ) => {
|
64
|
+
|
53
|
-
|
65
|
+
switch(action.type) {
|
54
|
-
|
55
|
-
|
66
|
+
|
56
|
-
|
57
|
-
|
67
|
+
case 'RECIPE_ADDED':
|
68
|
+
|
58
|
-
|
69
|
+
return {...state, recipes: action.payload}};
|
70
|
+
|
59
|
-
|
71
|
+
return state
|
72
|
+
|
73
|
+
|
60
74
|
|
61
75
|
}
|
62
76
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
const initialState = {
|
82
|
+
|
83
|
+
recipes:[
|
84
|
+
|
85
|
+
{title: '白米', image: '5c0cdd2fa192df5c7a968734d0c49d7a_s.png', author: 'Aさん', ing:'お米', amount:'1合', howto:'お米を水で研ぐ。'}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
};
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
export default AddRecipes;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
index.js/actions
|
102
|
+
|
103
|
+
```ここに言語を入力
|
104
|
+
|
105
|
+
import {combineReducers} from 'redux';
|
106
|
+
|
107
|
+
import AddRecipes from './AddRecipes';
|
108
|
+
|
109
|
+
import {reducer as formReducer} from 'redux-form';
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
const rootReducer = combineReducers({
|
118
|
+
|
119
|
+
form: formReducer,
|
120
|
+
|
121
|
+
recipes :AddRecipes
|
122
|
+
|
123
|
+
});
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
export default rootReducer;
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
Form.js/containers
|
136
|
+
|
137
|
+
```ここに言語を入力
|
138
|
+
|
139
|
+
import React, {Component} from 'react';
|
140
|
+
|
141
|
+
import {Field, reduxForm} from 'redux-form';
|
142
|
+
|
143
|
+
import { bindActionCreators } from 'redux';
|
144
|
+
|
145
|
+
import {handleSubmit} from '../actions/index';
|
146
|
+
|
147
|
+
import {addRecipe} from '../actions/index';
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
const Form = props => {
|
154
|
+
|
155
|
+
const {handleSubmit, pristine, submitting} = props;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
return(
|
160
|
+
|
161
|
+
<form onSubmit = {handleSubmit}>
|
162
|
+
|
163
|
+
<div>
|
164
|
+
|
165
|
+
<h3>新規レシピ</h3>
|
166
|
+
|
167
|
+
<p>タイトル</p>
|
168
|
+
|
169
|
+
<Field
|
170
|
+
|
171
|
+
name = 'newtitle'
|
172
|
+
|
173
|
+
label = 'newtitle'
|
174
|
+
|
175
|
+
component="input"
|
176
|
+
|
177
|
+
/>
|
178
|
+
|
179
|
+
<p>写真URL</p>
|
180
|
+
|
181
|
+
<Field
|
182
|
+
|
183
|
+
name = 'newimg'
|
184
|
+
|
185
|
+
label = 'newimg'
|
186
|
+
|
187
|
+
component="input"
|
188
|
+
|
189
|
+
/>
|
190
|
+
|
191
|
+
<p>材料</p>
|
192
|
+
|
193
|
+
<Field
|
194
|
+
|
195
|
+
name = 'newing'
|
196
|
+
|
197
|
+
label = 'newing'
|
198
|
+
|
199
|
+
component="input"
|
200
|
+
|
201
|
+
/>
|
202
|
+
|
203
|
+
<p>分量</p>
|
204
|
+
|
205
|
+
<Field
|
206
|
+
|
207
|
+
name = 'newamount'
|
208
|
+
|
209
|
+
label = 'newamount'
|
210
|
+
|
211
|
+
component="input"
|
212
|
+
|
213
|
+
/>
|
214
|
+
|
215
|
+
<p>作り方</p>
|
216
|
+
|
217
|
+
<Field
|
218
|
+
|
219
|
+
name = 'newstep'
|
220
|
+
|
221
|
+
label = 'newstep'
|
222
|
+
|
223
|
+
component="input"
|
224
|
+
|
225
|
+
/>
|
226
|
+
|
227
|
+
<p>コツ・コメント</p>
|
228
|
+
|
229
|
+
<Field
|
230
|
+
|
231
|
+
name = 'newcomment'
|
232
|
+
|
233
|
+
label = 'newcomment'
|
234
|
+
|
235
|
+
component="input"
|
236
|
+
|
237
|
+
/>
|
238
|
+
|
239
|
+
</div>
|
240
|
+
|
241
|
+
<div>
|
242
|
+
|
243
|
+
<button type = "submit" disabled = {pristine || submitting}>
|
244
|
+
|
245
|
+
送信
|
246
|
+
|
247
|
+
</button>
|
248
|
+
|
249
|
+
</div>
|
250
|
+
|
251
|
+
</form>
|
252
|
+
|
253
|
+
)
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
export default reduxForm({
|
260
|
+
|
261
|
+
form: 'addedrecipe',
|
262
|
+
|
263
|
+
fields: ['newtitle', 'newimg', 'newing','newamount','newstep', 'newcomment']
|
264
|
+
|
265
|
+
})(Form);
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
RecipeList.js/containers
|
272
|
+
|
273
|
+
```ここに言語を入力
|
274
|
+
|
275
|
+
import React, {Component} from 'react';
|
276
|
+
|
277
|
+
import {connect} from 'react-redux';
|
278
|
+
|
279
|
+
import {addRecipe} from '../actions/index';
|
280
|
+
|
281
|
+
import {handleSubmit} from '../actions/index';
|
282
|
+
|
283
|
+
import {bindActionCreators} from 'redux';
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
class RecipeList extends Component{
|
288
|
+
|
289
|
+
renderList(){
|
290
|
+
|
291
|
+
let handleSubmit = (values) => {
|
292
|
+
|
293
|
+
var data = {
|
294
|
+
|
295
|
+
newtitle:values.newtitle,
|
296
|
+
|
297
|
+
newimg: values.newimg,
|
298
|
+
|
299
|
+
newing: values.newing,
|
300
|
+
|
301
|
+
newamount: values.newamount,
|
302
|
+
|
303
|
+
newstep: values.newstep,
|
304
|
+
|
305
|
+
newcomment: values.newcomment
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
this.props.addRecipe(data)
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
return this.props.recipes.map((recipe)=>{
|
314
|
+
|
315
|
+
return(
|
316
|
+
|
317
|
+
<div onClick = {()=> this.props.activeRecipe(recipe)}>
|
318
|
+
|
319
|
+
<p>{recipe.title}</p>
|
320
|
+
|
321
|
+
</div>
|
322
|
+
|
323
|
+
);
|
324
|
+
|
325
|
+
});
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
render(){
|
332
|
+
|
333
|
+
return <ul>{this.renderList()}</ul>
|
88
334
|
|
89
335
|
}
|
90
336
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
con
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
export default AddRecipes;
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
```
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
index.js/actions
|
124
|
-
|
125
|
-
```ここに言語を入力
|
126
|
-
|
127
|
-
import {combineReducers} from 'redux';
|
128
|
-
|
129
|
-
import AddRecipes from './AddRecipes';
|
130
|
-
|
131
|
-
import ActiveRecipe from './ActiveRecipe';
|
132
|
-
|
133
|
-
import {reducer as formReducer} from 'redux-form';
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
const rootReducer = combineReducers({
|
142
|
-
|
143
|
-
activeRecipe : ActiveRecipe,
|
144
|
-
|
145
|
-
form: formReducer,
|
146
|
-
|
147
|
-
recipes :AddRecipes
|
148
|
-
|
149
|
-
});
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
export default rootReducer;
|
154
|
-
|
155
|
-
```
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
Form.js/containers
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
function mapStateToProps(state){
|
342
|
+
|
343
|
+
return {
|
344
|
+
|
345
|
+
recipes: state.recipes
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
export default connect (mapStateToProps, mapDispatchToProps)(RecipeList);
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
```
|
358
|
+
|
359
|
+
App.js/components
|
162
360
|
|
163
361
|
```ここに言語を入力
|
164
362
|
|
165
363
|
import React, {Component} from 'react';
|
166
364
|
|
167
|
-
import
|
365
|
+
import RecipeList from '../containers/RecipeList'
|
168
|
-
|
169
|
-
|
366
|
+
|
170
|
-
|
171
|
-
import
|
367
|
+
import Form from '../containers/Form'
|
172
|
-
|
368
|
+
|
173
|
-
import {addRecipe} from '../actions
|
369
|
+
import { addRecipe } from '../actions';
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
370
|
+
|
180
|
-
|
181
|
-
|
371
|
+
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
372
|
+
|
186
|
-
|
187
|
-
<form onSubmit = {handleSubmit}>
|
188
|
-
|
189
|
-
<div>
|
190
|
-
|
191
|
-
<h3>新規レシピ</h3>
|
192
|
-
|
193
|
-
<p>タイトル</p>
|
194
|
-
|
195
|
-
<Field
|
196
|
-
|
197
|
-
name = 'newtitle'
|
198
|
-
|
199
|
-
label = 'newtitle'
|
200
|
-
|
201
|
-
component="input"
|
202
|
-
|
203
|
-
/>
|
204
|
-
|
205
|
-
<p>写真URL</p>
|
206
|
-
|
207
|
-
<Field
|
208
|
-
|
209
|
-
name = 'newimg'
|
210
|
-
|
211
|
-
label = 'newimg'
|
212
|
-
|
213
|
-
component="input"
|
214
|
-
|
215
|
-
/>
|
216
|
-
|
217
|
-
<p>材料</p>
|
218
|
-
|
219
|
-
<Field
|
220
|
-
|
221
|
-
name = 'newing'
|
222
|
-
|
223
|
-
label = 'newing'
|
224
|
-
|
225
|
-
component="input"
|
226
|
-
|
227
|
-
/>
|
228
|
-
|
229
|
-
<p>分量</p>
|
230
|
-
|
231
|
-
<Field
|
232
|
-
|
233
|
-
name = 'newamount'
|
234
|
-
|
235
|
-
label = 'newamount'
|
236
|
-
|
237
|
-
component="input"
|
238
|
-
|
239
|
-
/>
|
240
|
-
|
241
|
-
<p>作り方</p>
|
242
|
-
|
243
|
-
<Field
|
244
|
-
|
245
|
-
name = 'newstep'
|
246
|
-
|
247
|
-
label = 'newstep'
|
248
|
-
|
249
|
-
component="input"
|
250
|
-
|
251
|
-
/>
|
252
|
-
|
253
|
-
<p>コツ・コメント</p>
|
254
|
-
|
255
|
-
<Field
|
256
|
-
|
257
|
-
name = 'newcomment'
|
258
|
-
|
259
|
-
label = 'newcomment'
|
260
|
-
|
261
|
-
component="input"
|
262
|
-
|
263
|
-
/>
|
264
|
-
|
265
|
-
</div>
|
266
|
-
|
267
|
-
<div>
|
268
|
-
|
269
|
-
<button type = "submit" disabled = {pristine || submitting}>
|
270
|
-
|
271
|
-
送信
|
272
|
-
|
273
|
-
</button>
|
274
|
-
|
275
|
-
</div>
|
276
|
-
|
277
|
-
</form>
|
278
|
-
|
279
|
-
)
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
export default reduxForm({
|
286
|
-
|
287
|
-
form: 'addedrecipe',
|
288
|
-
|
289
|
-
fields: ['newtitle', 'newimg', 'newing','newamount','newstep', 'newcomment']
|
290
|
-
|
291
|
-
})(Form);
|
292
|
-
|
293
|
-
```
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
RecipeList.js/containers
|
298
|
-
|
299
|
-
```ここに言語を入力
|
300
|
-
|
301
|
-
import React, {Component} from 'react';
|
302
|
-
|
303
|
-
import {connect} from 'react-redux';
|
304
|
-
|
305
|
-
import {activeRecipe} from '../actions/index';
|
306
|
-
|
307
|
-
import {addRecipe} from '../actions/index';
|
308
|
-
|
309
|
-
import {handleSubmit} from '../actions/index';
|
310
|
-
|
311
|
-
import {bindActionCreators} from 'redux';
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
class
|
373
|
+
export default class App extends Component{
|
316
|
-
|
317
|
-
renderList(){
|
318
|
-
|
319
|
-
let handleSubmit = (values) => {
|
320
|
-
|
321
|
-
var data = {
|
322
|
-
|
323
|
-
newtitle:values.newtitle,
|
324
|
-
|
325
|
-
newimg: values.newimg,
|
326
|
-
|
327
|
-
newing: values.newing,
|
328
|
-
|
329
|
-
newamount: values.newamount,
|
330
|
-
|
331
|
-
newstep: values.newstep,
|
332
|
-
|
333
|
-
newcomment: values.newcomment
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
this.props.addRecipe(data)
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
return this.props.recipes.map((recipe)=>{
|
342
|
-
|
343
|
-
return(
|
344
|
-
|
345
|
-
<div onClick = {()=> this.props.activeRecipe(recipe)}>
|
346
|
-
|
347
|
-
<p>{recipe.title}</p>
|
348
|
-
|
349
|
-
<p>by {recipe.author}</p>
|
350
|
-
|
351
|
-
<p>材料 {recipe.ing.split(',').slice(0, 3).join('、 ')}</p>
|
352
|
-
|
353
|
-
<img src={require(`../pictures/${recipe.image}`)} style={{ width: 80, height: 60 }}/>
|
354
|
-
|
355
|
-
</div>
|
356
|
-
|
357
|
-
);
|
358
|
-
|
359
|
-
});
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
|
364
374
|
|
365
375
|
render(){
|
366
376
|
|
367
|
-
return <ul>{this.renderList()}</ul>
|
368
|
-
|
369
|
-
}
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
function mapStateToProps(state){
|
376
|
-
|
377
|
-
return {
|
378
|
-
|
379
|
-
recipes: state.recipes
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
}
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
function mapDispatchToProps(dispatch){
|
388
|
-
|
389
|
-
return bindActionCreators({activeRecipe: activeRecipe}, dispatch)
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
export default connect (mapStateToProps, mapDispatchToProps)(RecipeList);
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
```
|
400
|
-
|
401
|
-
App.js/components
|
402
|
-
|
403
|
-
```ここに言語を入力
|
404
|
-
|
405
|
-
import React, {Component} from 'react';
|
406
|
-
|
407
|
-
import RecipeList from '../containers/RecipeList'
|
408
|
-
|
409
|
-
import RecipeDetail from '../containers/RecipeDetail'
|
410
|
-
|
411
|
-
import Form from '../containers/Form'
|
412
|
-
|
413
|
-
import { addRecipe } from '../actions';
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
export default class App extends Component{
|
418
|
-
|
419
|
-
render(){
|
420
|
-
|
421
377
|
return (
|
422
378
|
|
423
379
|
<div>
|
@@ -426,8 +382,6 @@
|
|
426
382
|
|
427
383
|
<RecipeList />
|
428
384
|
|
429
|
-
<RecipeDetail />
|
430
|
-
|
431
385
|
</div>
|
432
386
|
|
433
387
|
);
|
@@ -458,9 +412,7 @@
|
|
458
412
|
|
459
413
|
|
460
414
|
|
461
|
-
まずはinitialStateを渡してRecipeListに反映させ
|
415
|
+
まずはinitialStateを渡してRecipeListに反映させたいです。
|
462
|
-
|
463
|
-
また可能であれば、その後Formに内容を入力して送信した時のonSubmitのイベント部分のコードも一緒に確認して頂きたいです。
|
464
416
|
|
465
417
|
|
466
418
|
|