質問編集履歴

1

アドバイスを受け無事解決したので、結果を書きました。

2020/05/18 15:35

投稿

DaisukeMori
DaisukeMori

スコア226

test CHANGED
File without changes
test CHANGED
@@ -179,3 +179,281 @@
179
179
  export default Index;
180
180
 
181
181
  ```
182
+
183
+
184
+
185
+ # 解決方法
186
+
187
+ アドバイスを受け無事表示できたので結果を載せておきます。
188
+
189
+
190
+
191
+ ## 【うまくいった方法】
192
+
193
+ js側で投稿記事を抽出するロジックを書く
194
+
195
+ jsx側では表示のみにする
196
+
197
+
198
+
199
+ ```jsx
200
+
201
+ // js側で抽出
202
+
203
+ // auth userの投稿記事のみ抽出
204
+
205
+ const result = list.filter((e) => {
206
+
207
+ return e.authId === authId;
208
+
209
+ });
210
+
211
+ console.log(result);
212
+
213
+ const length = result.length;
214
+
215
+ console.log(length);
216
+
217
+
218
+
219
+ {/* jsx側で表示 */}
220
+
221
+ <div className="post-list user-post-list">
222
+
223
+ {length !== 0 ? (
224
+
225
+ <React.Fragment>
226
+
227
+ {result.map(item => (
228
+
229
+ <div key={item.docId + String(new Date())}>
230
+
231
+ <div className="auth-inner-post-list">
232
+
233
+ <div className="auth-inner-post-text">
234
+
235
+ <div className="post-msg">{item.msg}</div>
236
+
237
+ <span className="delete" onClick={() => handleDelete(item.docId)}>&gt; Delete...</span>
238
+
239
+ </div>
240
+
241
+ </div>
242
+
243
+ </div>
244
+
245
+ ))}
246
+
247
+ </React.Fragment>
248
+
249
+ ) : (
250
+
251
+ <div className="no-user-posts">まだ記事が投稿されていません</div>
252
+
253
+ )}
254
+
255
+ </div>
256
+
257
+ ```
258
+
259
+
260
+
261
+ ```jsx
262
+
263
+ // コード全文
264
+
265
+ import React from 'react';
266
+
267
+ import { Link } from 'react-router-dom';
268
+
269
+ import firebase, { db } from '../../Firebase';
270
+
271
+ import SignOut from '../FirebaseAuthHook/SignOut';
272
+
273
+ import { useCollectionData } from "react-firebase-hooks/firestore";
274
+
275
+ import BackTopIcon from '../../assets/img/backtop.png';
276
+
277
+ import UserIcon from '../../assets/img/user.png';
278
+
279
+ import styled from 'styled-components';
280
+
281
+
282
+
283
+ const BackTopStyle = styled.img`
284
+
285
+ width: 50px;
286
+
287
+ top: 25px;
288
+
289
+ left: 25px;
290
+
291
+ position: fixed;
292
+
293
+ zIndex: 11,
294
+
295
+ `;
296
+
297
+
298
+
299
+ const Index = () => {
300
+
301
+ // Firebese Auth uid, email取得
302
+
303
+ const user = firebase.auth().currentUser;
304
+
305
+ let authId;
306
+
307
+ let name;
308
+
309
+ let photoURL;
310
+
311
+ if (user !== null) {
312
+
313
+ user.providerData.forEach(() => {
314
+
315
+ authId = user.uid;
316
+
317
+ name = user.displayName;
318
+
319
+ photoURL = user.photoURL;
320
+
321
+ });
322
+
323
+ }
324
+
325
+
326
+
327
+ // Render
328
+
329
+ const [ list, loading, error ] = useCollectionData(db.collection('posts').orderBy('createdAt', 'desc'), { idField: 'docId' });
330
+
331
+ if (loading) return <div>Loading...</div>;
332
+
333
+ if (error) return <div>Error...</div>;
334
+
335
+
336
+
337
+ // auth userの投稿記事のみ抽出
338
+
339
+ const result = list.filter((e) => {
340
+
341
+ return e.authId === authId;
342
+
343
+ });
344
+
345
+ console.log(result);
346
+
347
+ const length = result.length;
348
+
349
+ console.log(length);
350
+
351
+
352
+
353
+
354
+
355
+ // Delete
356
+
357
+ const handleDelete = (uid) => {
358
+
359
+ if (window.confirm('削除しますか?')) {
360
+
361
+ db.collection('posts').doc(uid).delete();
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+ return(
370
+
371
+ <React.Fragment>
372
+
373
+ <SignOut />
374
+
375
+ <Link to="/"><BackTopStyle src={ BackTopIcon } alt="Topに戻る"/></Link>
376
+
377
+
378
+
379
+ <div className="user-wrapper">
380
+
381
+ <div className="user">
382
+
383
+ {photoURL ? (
384
+
385
+ <img src={ photoURL } className="auth-user-icon" alt="User Thumbnail" />
386
+
387
+ ) : (
388
+
389
+ <img src={ UserIcon } className="auth-user-icon" alt="Firebase Thumbnail" />
390
+
391
+ )}
392
+
393
+ <div className="user-datail">
394
+
395
+ {name ? (
396
+
397
+ <div className="user-name">{name}</div>
398
+
399
+ ) : (
400
+
401
+ <div className="user-name">Firebaseユーザー</div>
402
+
403
+ )}
404
+
405
+ </div>
406
+
407
+ </div>
408
+
409
+
410
+
411
+ <div className="post-list user-post-list">
412
+
413
+ {length !== 0 ? (
414
+
415
+ <React.Fragment>
416
+
417
+ {result.map(item => (
418
+
419
+ <div key={item.docId + String(new Date())}>
420
+
421
+ <div className="auth-inner-post-list">
422
+
423
+ <div className="auth-inner-post-text">
424
+
425
+ <div className="post-msg">{item.msg}</div>
426
+
427
+ <span className="delete" onClick={() => handleDelete(item.docId)}>&gt; Delete...</span>
428
+
429
+ </div>
430
+
431
+ </div>
432
+
433
+ </div>
434
+
435
+ ))}
436
+
437
+ </React.Fragment>
438
+
439
+ ) : (
440
+
441
+ <div className="no-user-posts">まだ記事が投稿されていません</div>
442
+
443
+ )}
444
+
445
+ </div>
446
+
447
+ </div>
448
+
449
+ </React.Fragment>
450
+
451
+ );
452
+
453
+ }
454
+
455
+
456
+
457
+ export default Index;
458
+
459
+ ```