Skip to content

Commit

Permalink
[๐Ÿ’Ž : refactor] userTimeline onSnap ์ œํ•œ ๊ฑธ๊ธฐ (#12)
Browse files Browse the repository at this point in the history
- ์œ ์‚ฌํ•œ ์ž‘์—…์„ timeline๋•Œ ์ ์šฉํ•œ ์  ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ  (d37a720)
- onSnapshot์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ firestore์˜ ์ •๋ณด๋ฅผ readํ•ด์™€ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ถ„๋‹น 1์ฒœํšŒ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ๊ธˆ์˜ ์ฃผ์›์ธ์ด ๋จ
- onSnapshot๊ณผ fetchTweets ๊ณผ์ •์„ useEffect ๋‚ด๋ถ€๋กœ ์ด๋™์‹œ์ผœ cleanup ๊ธฐ๋Šฅ์— ์˜์กดํ•˜๋Š” ๋ฐฉ์‹
  • Loading branch information
sryung1225 committed Dec 24, 2023
1 parent bb57c2f commit c7752c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useState } from 'react';
import { Unsubscribe } from 'firebase/auth';
import {
collection,
limit,
onSnapshot,
orderBy,
query,
} from 'firebase/firestore';
import { Unsubscribe } from 'firebase/auth';
import { db } from '../firebase.ts';
import Tweet from './tweet.tsx';
import * as S from '../styles/timeline.ts';
Expand Down
54 changes: 31 additions & 23 deletions src/components/user-timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useState } from 'react';
import { Unsubscribe } from 'firebase/auth';
import {
collection,
getDocs,
limit,
onSnapshot,
orderBy,
query,
where,
Expand All @@ -15,30 +16,37 @@ import ITweet from '../interfaces/ITweet.ts';
export default function UserTimeline() {
const user = auth.currentUser;
const [tweets, setTweets] = useState<ITweet[]>([]);
const fetchTweets = async () => {
const tweetsQuery = query(
collection(db, 'tweets'),
where('userId', '==', user?.uid),
orderBy('createdAt', 'desc'),
limit(25),
);
const snapshot = await getDocs(tweetsQuery);
const tweetList = snapshot.docs.map((doc) => {
const { userId, userName, tweet, createdAt, photo } = doc.data();
return {
userId,
userName,
tweet,
createdAt,
photo,
id: doc.id,
};
});
setTweets(tweetList);
};
useEffect(() => {
let unsubscribe: Unsubscribe | null = null;
const fetchTweets = async () => {
const tweetsQuery = query(
collection(db, 'tweets'),
where('userId', '==', user?.uid),
orderBy('createdAt', 'desc'),
limit(25),
);
unsubscribe = await onSnapshot(tweetsQuery, (snapshot) => {
const tweetList = snapshot.docs.map((doc) => {
const { userId, userName, tweet, createdAt, photo } = doc.data();
return {
userId,
userName,
tweet,
createdAt,
photo,
id: doc.id,
};
});
setTweets(tweetList);
});
};
fetchTweets();
}, [tweets]);
return () => {
if (unsubscribe) {
unsubscribe();
}
};
}, []);
return (
<S.TimelineWrapper>
{tweets.map((tweet) => (
Expand Down

1 comment on commit c7752c1

@sryung1225
Copy link
Owner Author

@sryung1225 sryung1225 commented on c7752c1 Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ‘ฟ ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€ ์ •์ •

user-timeline์€ tweets ์ปฌ๋ ‰์…˜์˜ ๋ฌธ์„œ๋ฅผ getDocs ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ตœ์ดˆ ํ•œ ๋ฒˆ๋งŒ ๊ฐ€์ ธ์˜ค๋„๋ก ์„ค๊ณ„ ํ–ˆ์—ˆ๋‹ค

  • getDocs : ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๊ฐ€์ ธ์˜ด
  • onSnapshot : ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐ€์ ธ์˜ด

์ฆ‰, firebase๋ฅผ ๊ณผ๋„ํ•˜๊ฒŒ readํ•˜๋Š” ๋ฌธ์ œ๋Š” onSnapshot์„ ์ ์šฉํ•˜๊ธฐ ์ด์ „์— ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์˜€๊ธฐ ๋•Œ๋ฌธ์—
onSnapshot์ด ๋ฌธ์ œ์˜ ์›์ธ์ด๋ผ๊ณ  ์ž‘์„ฑํ•œ ๊ฒƒ์€ ์™„์ „ํžˆ ์ž˜๋ชป๋œ ๋‚ด์šฉ

์‹ค์ œ ๋ฌธ์ œ์˜ ์›์ธ์€ useEffect์˜ dependency

useEffect(() => {
    fetchTweets();
}, [tweets]);

useEffect์— ์ž‘์„ฑํ•œ fetchTweets()๋Š” ์ตœ์ดˆ mount ์‹œ ์‹คํ–‰๋˜์–ด tweets state์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•œ๋‹ค
๊ทธ๋Ÿฐ๋ฐ dependency๋กœ tweets๋ฅผ ์ž‘์„ฑํ•œ ๊ฒƒ์€ tweets๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋งˆ๋‹ค useEffect์— ์ž‘์„ฑํ•œ ๋‚ด์šฉ์„ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ
๋•Œ๋ฌธ์— ๋‹ค์‹œ fetchTweets()๊ฐ€ ์‹คํ–‰๋˜๊ณ  tweets์˜ ๋ณ€๊ฒฝ๋˜๋Š” ๋ฌดํ•œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

dependency๋ฅผ ๋น„์›Œ์ฃผ๊ฒŒ ๋˜๋ฉด ์˜๋„๋Œ€๋กœ ํ•œ ๋ฒˆ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ๋‹ค

useEffect(() => {
    fetchTweets();
}, []);

onSnapshot ์ ์šฉ์€ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ์—๋Š” ์ƒ๊ด€์ด ์—†์—ˆ์œผ๋ฉฐ
์‹ค์‹œ๊ฐ„ ํƒ€์ž„๋ผ์ธ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ง„ํ–‰๋œ ๋ณ„๊ฐœ ์ž‘์—…์œผ๋กœ ๋‚ด์šฉ์„ ์ •์ •ํ•œ๋‹ค.


After

[๐Ÿ›  : fix] userTimeline์˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌดํ•œ ๋ฐ˜๋ณต ๋ฌธ์ œ ํ•ด๊ฒฐ (#12)

  • useEffect์˜ dependency๋กœ ์ธํ•ด fetchTweets๊ฐ€ ๋ฌดํ•œ ๋ฐ˜๋ณต. ์ด๋กœ ์ธํ•ด /profile์„ ์—ด๋žŒํ•œ ์ฑ„๋กœ ๊ทธ๋Œ€๋กœ ๋‘๊ธฐ๋งŒ ํ•ด๋„ firestore์˜ ๋ฌธ์„œ๋ฅผ ๊ณ„์† readํ•ด์™€ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ถ„๋‹น 1์ฒœํšŒ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ๊ธˆ์˜ ์ฃผ์›์ธ์ด ๋จ
  • dependency๋ฅผ ๋น„์›Œ์คŒ์œผ๋กœ์จ ์ปดํฌ๋„ŒํŠธ๋ฅผ mountํ•˜๋Š” ์ตœ์ดˆ 1ํšŒ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•จ

[๐Ÿฅ : feat] ์œ ์ € ํƒ€์ž„๋ผ์ธ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ

  • getDocs ๋Œ€์‹  onSnapshot์„ ์ด์šฉํ•˜๋ฉด์„œ ์ตœ์ดˆ 1ํšŒ์—์„œ ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€๋™์‚ฌํ•ญ์„ ์ถ”์ ํ•˜๋„๋ก ์ˆ˜์ •
  • ์‹ค์‹œ๊ฐ„ ํƒ€์ž„๋ผ์ธ์— ๋Œ€ํ•ด์„œ๋Š” ์œ ์‚ฌํ•œ ์ž‘์—…์„ timeline์—์„œ ์ ์šฉํ•œ ์  ์žˆ์œผ๋‹ˆ ์ปค๋ฐ‹ ์ฐธ๊ณ  (d37a720)

Please sign in to comment.