質問編集履歴

3

ソースコードを追記して、指摘箇所を訂正しました。

2018/04/20 07:57

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,20 @@
16
16
 
17
17
  #### 実現したいこと&困っていること
18
18
 
19
+
20
+
21
+ ```
22
+
23
+ ReferenceError: message is not defined
24
+
25
+ ```
26
+
27
+ Message.jsの`messages: GiftedChat.append(previousState.messages, message),`でつっかえています。ここには、既にFirebaseに保存されているデータベースが取ってこれるはずなのですが、このmessageを取ってくることができなくて困っています。
28
+
29
+
30
+
31
+
32
+
19
33
  [こちらのソースコード](https://github.com/FaridSafi/ChatApp)をRedux仕様にして、チャット機能を実装したいのですが、保存は成功しているのですが
20
34
 
21
35
 
@@ -24,11 +38,15 @@
24
38
 
25
39
 
26
40
 
27
- Fetchがうまくいきません。
28
-
29
- 参考コードに忠実にしたがっているのですが、やはりmessageをfetchできません。
30
-
31
- ComoponentDidMount効いていないように思います。
41
+ message2は下記のコンソール情報取っこれています。
42
+
43
+
44
+
45
+ ![イメージ説明](de07bfea46f399ec4ad8cc9e5e057c73.png)
46
+
47
+
48
+
49
+ ここのvalueを一つずつmessageの中にsetStateしてあげたいのですが><
32
50
 
33
51
 
34
52
 
@@ -60,6 +78,8 @@
60
78
 
61
79
 
62
80
 
81
+ console.log('message3, message);
82
+
63
83
  this.setState((previousState) => {
64
84
 
65
85
  return {
@@ -272,22 +292,136 @@
272
292
 
273
293
 
274
294
 
275
- meesage1, message2は取れて来ていますが、message3はエラーすら出ません(無視されている?)
276
-
277
-
278
-
279
- #### 追記
280
-
281
-
282
-
283
- componentDidMountを排除し、componentWillMountを使うことにしました。
284
-
285
- message2は下記のコンソール情報が取ってこれています。
286
-
287
-
288
-
289
- ![イメージ説明](de07bfea46f399ec4ad8cc9e5e057c73.png)
290
-
291
-
292
-
293
- ここのvalueを一つずつmessageの中にsetStateしてあげたいのですが><
295
+ > messageReducer.js
296
+
297
+
298
+
299
+ ```JavaScript
300
+
301
+ type uiActions = {
302
+
303
+ type: 'messages_fetch_success',
304
+
305
+ }
306
+
307
+
308
+
309
+ const initialState = {};
310
+
311
+
312
+
313
+ export default (state = initialState, action) => {
314
+
315
+ switch (action.type) {
316
+
317
+ case 'messages_fetch_success':
318
+
319
+ return action.payload;
320
+
321
+ default:
322
+
323
+ return state;
324
+
325
+ }
326
+
327
+ };
328
+
329
+ ```
330
+
331
+
332
+
333
+ > messageFormReducer.js
334
+
335
+
336
+
337
+ ```
338
+
339
+ /**
340
+
341
+ * @flow
342
+
343
+ *
344
+
345
+ * Manages the state of all Message Form components which rely on Redux
346
+
347
+ */
348
+
349
+
350
+
351
+ type uiActions = {
352
+
353
+ type: 'message_update',
354
+
355
+ type: 'message_create,'
356
+
357
+ }
358
+
359
+
360
+
361
+ const initialState = {
362
+
363
+ text: '',
364
+
365
+ user: '',
366
+
367
+ createdAt: '',
368
+
369
+ _id: '',
370
+
371
+ roomId: ''
372
+
373
+ };
374
+
375
+
376
+
377
+ export default (state = initialState, action) => {
378
+
379
+ switch(action.type) {
380
+
381
+ case 'message_update':
382
+
383
+ return { ...state, [action.payload.prop]: action.payload.value };
384
+
385
+ case 'message_create':
386
+
387
+ return initialState;
388
+
389
+ default:
390
+
391
+ return state;
392
+
393
+ }
394
+
395
+ };
396
+
397
+ ```
398
+
399
+
400
+
401
+ > store.js
402
+
403
+
404
+
405
+ ```
406
+
407
+ const rootReducer = combineReducers({
408
+
409
+ messages: messageReducer,
410
+
411
+ messageForm: messageFormReducer
412
+
413
+ });
414
+
415
+
416
+
417
+ const store = createStore(
418
+
419
+ rootReducer,
420
+
421
+ applyMiddleware(reduxThunk)
422
+
423
+ );
424
+
425
+ export default store;
426
+
427
+ ```

2

ソースコードの修正、エラーの詳細を追記しました

2018/04/20 07:57

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -8,9 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- [Firebaseでチャット機能を実現したい(react native)](https://teratail.com/questions/122414
11
+ [Firebaseでチャット機能を実現したい(react native)](https://teratail.com/questions/121827)
12
-
13
- )
14
12
 
15
13
  [gifted chatでmessageをDBからFetchしたい(react native)](https://teratail.com/questions/122414)
16
14
 
@@ -54,226 +52,242 @@
54
52
 
55
53
 
56
54
 
57
- componentDidMount() {
58
-
59
- this.props.messagesFetch((message) => {
60
-
61
- console.log('message3', message);
62
-
63
- this.setState((previousState) => {
64
-
65
- return {
66
-
67
- messages: GiftedChat.append(previousState.messages, message),
68
-
69
- };
70
-
71
- });
55
+ componentWillMount() {
56
+
57
+ const room = this.props.navigation.state.params.room;
58
+
59
+ this.props.messagesFetch(room);
60
+
61
+
62
+
63
+ this.setState((previousState) => {
64
+
65
+ return {
66
+
67
+ messages: GiftedChat.append(previousState.messages, message),
68
+
69
+ };
70
+
71
+ });
72
+
73
+ }
74
+
75
+
76
+
77
+ onSend = (message) => {
78
+
79
+ const room = this.props.navigation.state.params.room;
80
+
81
+ this.props.sendMessage({
82
+
83
+ message,
84
+
85
+ room
86
+
87
+ });
88
+
89
+ }
90
+
91
+
92
+
93
+ renderBubble(props) {
94
+
95
+ return (
96
+
97
+ <Bubble
98
+
99
+ {...props}
100
+
101
+ wrapperStyle={{
102
+
103
+ right: {
104
+
105
+ backgroundColor: Theme.SECONDARY
106
+
107
+ }
108
+
109
+ }}
110
+
111
+ />
112
+
113
+ )
114
+
115
+ }
116
+
117
+
118
+
119
+ renderSend(props) {
120
+
121
+ return (
122
+
123
+ <Send
124
+
125
+ {...props}
126
+
127
+ textStyle={{
128
+
129
+ color: Theme.SECONDARY
130
+
131
+ }}
132
+
133
+ />
134
+
135
+ );
136
+
137
+ }
138
+
139
+
140
+
141
+ render() {
142
+
143
+ const { currentUser } = firebase.auth();
144
+
145
+ return (
146
+
147
+ <Screen>
148
+
149
+ <GiftedChat
150
+
151
+ messages={this.state.messages}
152
+
153
+ onSend={this.onSend.bind(this)}
154
+
155
+ user={{
156
+
157
+ _id: currentUser.uid
158
+
159
+ }}
160
+
161
+ renderBubble={this.renderBubble}
162
+
163
+ renderSend={this.renderSend}
164
+
165
+ />
166
+
167
+ </Screen>
168
+
169
+ );
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ const mapStateToProps = (state) => {
178
+
179
+ const { text, user, createdAt, _id } = state.messageForm;
180
+
181
+ return { text, user, createdAt, _id };
182
+
183
+ };
184
+
185
+
186
+
187
+ export default connect(mapStateToProps, {
188
+
189
+ sendMessage,
190
+
191
+ messagesFetch
192
+
193
+ })(Message);
194
+
195
+ ```
196
+
197
+ > redux/messageActions.js
198
+
199
+
200
+
201
+ ```JavaScript
202
+
203
+ /**
204
+
205
+ * Builds an action to request
206
+
207
+ */
208
+
209
+ export const sendMessage = ({ message, room }) => {
210
+
211
+ const { currentUser } = firebase.auth();
212
+
213
+ return (dispatch) => {
214
+
215
+ firebase.database().ref(`/rooms/${room.roomId}/messages/`)
216
+
217
+ .push({
218
+
219
+ text: message[0].text,
220
+
221
+ user: message[0].user,
222
+
223
+ createdAt: firebase.database.ServerValue.TIMESTAMP,
224
+
225
+ })
226
+
227
+ .then(() => {
228
+
229
+ dispatch({ type: 'message_create' });
230
+
231
+ });
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ /**
240
+
241
+ * Builds an action to notification fetch action
242
+
243
+ */
244
+
245
+ export const messagesFetch = (room) => {
246
+
247
+ return (dispatch) => {
248
+
249
+ firebase.database().ref(`/rooms/${room.roomId}/messages/`)
250
+
251
+ .once('value', snapshot => {
252
+
253
+ const message = snapshot.val();
254
+
255
+ return message;
256
+
257
+ })
258
+
259
+ .then(message => {
260
+
261
+ dispatch({ type: 'messages_fetch_success', payload: message });
262
+
263
+ console.log('message2', message);
72
264
 
73
265
  });
74
266
 
75
267
  };
76
268
 
77
-
78
-
79
- onSend = (message) => {
80
-
81
- const room = this.props.navigation.state.params.room;
82
-
83
- this.props.sendMessage({
84
-
85
- message,
86
-
87
- room
88
-
89
- });
90
-
91
- }
92
-
93
-
94
-
95
- renderBubble(props) {
96
-
97
- return (
98
-
99
- <Bubble
100
-
101
- {...props}
102
-
103
- wrapperStyle={{
104
-
105
- right: {
106
-
107
- backgroundColor: Theme.SECONDARY
108
-
109
- }
110
-
111
- }}
112
-
113
- />
114
-
115
- )
116
-
117
- }
118
-
119
-
120
-
121
- renderSend(props) {
122
-
123
- return (
124
-
125
- <Send
126
-
127
- {...props}
128
-
129
- textStyle={{
130
-
131
- color: Theme.SECONDARY
132
-
133
- }}
134
-
135
- />
136
-
137
- );
138
-
139
- }
140
-
141
-
142
-
143
- render() {
144
-
145
- const { currentUser } = firebase.auth();
146
-
147
- return (
148
-
149
- <Screen>
150
-
151
- <GiftedChat
152
-
153
- messages={this.state.messages}
154
-
155
- onSend={this.onSend.bind(this)}
156
-
157
- user={{
158
-
159
- _id: currentUser.uid
160
-
161
- }}
162
-
163
- renderBubble={this.renderBubble}
164
-
165
- renderSend={this.renderSend}
166
-
167
- />
168
-
169
- </Screen>
170
-
171
- );
172
-
173
- }
174
-
175
- }
176
-
177
-
178
-
179
- const mapStateToProps = (state) => {
180
-
181
- const { text, user, createdAt, _id } = state.messageForm;
182
-
183
- return { text, user, createdAt, _id };
184
-
185
269
  };
186
270
 
187
-
188
-
189
- export default connect(mapStateToProps, {
190
-
191
- sendMessage,
192
-
193
- messagesFetch
194
-
195
- })(Message);
196
-
197
271
  ```
198
272
 
199
- > redux/messageActions.js
200
-
201
-
202
-
203
- ```JavaScript
204
-
205
- /**
206
-
207
- * Builds an action to request
208
-
209
- */
210
-
211
- export const sendMessage = ({ message, room }) => {
212
-
213
- const { currentUser } = firebase.auth();
214
-
215
- return (dispatch) => {
216
-
217
- firebase.database().ref(`/rooms/${room.roomId}/messages/`)
218
-
219
- .push({
220
-
221
- text: message[0].text,
222
-
223
- user: message[0].user,
224
-
225
- createdAt: firebase.database.ServerValue.TIMESTAMP,
226
-
227
- })
228
-
229
- .then(() => {
230
-
231
- dispatch({ type: 'message_create' });
232
-
233
- });
234
-
235
- }
236
-
237
- }
238
-
239
-
240
-
241
- /**
242
-
243
- * Builds an action to notification fetch action
244
-
245
- */
246
-
247
- export const messagesFetch = (room) => {
248
-
249
- return (dispatch) => {
250
-
251
- firebase.database().ref(`/rooms/${room.roomId}/messages/`)
252
-
253
- .once('value', snapshot => {
254
-
255
- const message = snapshot.val();
256
-
257
- return message;
258
-
259
- console.log('message1', message);
260
-
261
- })
262
-
263
- .then(message => {
264
-
265
- dispatch({ type: 'messages_fetch_success', payload: message });
266
-
267
- console.log('message2', message);
268
-
269
- });
270
-
271
- };
272
-
273
- };
274
-
275
- ```
276
-
277
273
 
278
274
 
279
275
  meesage1, message2は取れて来ていますが、message3はエラーすら出ません(無視されている?)
276
+
277
+
278
+
279
+ #### 追記
280
+
281
+
282
+
283
+ componentDidMountを排除し、componentWillMountを使うことにしました。
284
+
285
+ message2は下記のコンソール情報が取ってこれています。
286
+
287
+
288
+
289
+ ![イメージ説明](de07bfea46f399ec4ad8cc9e5e057c73.png)
290
+
291
+
292
+
293
+ ここのvalueを一つずつmessageの中にsetStateしてあげたいのですが><

1

プライベートな情報を排除しました!

2018/04/19 21:10

投稿

yamady
yamady

スコア176

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  react nativeでチャット投稿を実現したいと思っています。
6
6
 
7
- (これで3回目の質問になります。Hayatoさんいつもありがとうございます!泣
7
+ (これで3回目の質問になります。)
8
8
 
9
9
 
10
10