https://nomadcoders.co/nwitter
트위터 클론코딩 – 노마드 코더 Nomad Coders
React Firebase for Beginners
nomadcoders.co
*노마드코더 트위터 클론코딩 강의내용을 정리한 글입니다.
**강의에서는 Firebase v8 를 사용했지만 여기서는 Firebase v9을 사용하여 구현했습니다.
https://firebase.google.com/docs/firestore/query-data/queries
https://firebase.google.com/docs/firestore/query-data/order-limit-data
// Profile.js
// ...
const getMyNweets = async () => {
// 내가 작성한 데이터를 시간순으로 오름차순 정렬하여 가져옴
const q = query(
collection(fbStore, "nweets"),
where("creatorId", "==", userInfo.uid),
orderBy("createdAt")
);
const nweets = await getDocs(q);
console.log(nweets.docs.map((doc) => doc.data()));
};
useEffect(() => {
getMyNweets();
}, []);
// ...
https://console.firebase.google.com/project/nwitter-9aeb9/firestore/indexes
// Profile.js
// ...
const onSubmitHandler = async (event) => {
event.preventDefault();
if (userInfo.displayName === newDisplayName) return;
await updateProfile(userInfo, { displayName: newDisplayName, photoURL: "" });
};
// ...
// App.js
// ...
const refreshUser = () => {
setUserInfo(fbAuth.currentUser);
};
// ...
return (
<div>
{loading ? `loading...` : <Router refreshUser={refreshUser} isLoggedIn={isLoggedIn} userInfo={userInfo} />}
<footer>© {new Date().getFullYear()} Nwitter</footer>
</div>
);
// ...
// Profile.js
// ...
const onSubmitHandler = async (event) => {
event.preventDefault();
if (userInfo.displayName === newDisplayName) return;
await updateProfile(userInfo, { displayName: newDisplayName, photoURL: "" });
refreshUser();
};
// ...
⇒ userInfo는 방대한 정보를 담고있는 객체이기 때문에 react에서 변경점을 찾지 못해 재랜더링이 되지 않을 수 있음
⇒ 해결법
// App.js
// ...
const refreshUser = () => {
// setUserInfo(fbAuth.currentUser);
const user = fbAuth.currentUser;
// 실제 사용할 정보만 userInfo에 저장해서 사용
setUserInfo({
displayName: user.displayName,
uid: user.uid,
updateProfile: (args) => user.updateProfile(args),
});
};
useEffect(() => {
onAuthStateChanged(fbAuth, (user) => {
if (user) {
setIsLoggedIn(true);
// setUserInfo(user);
setUserInfo({
displayName: user.displayName,
uid: user.uid,
updateProfile: (args) => user.updateProfile(args),
});
} else {
setIsLoggedIn(false);
setUserInfo(null);
}
setLoading(false);
});
}, []);
// ...
⇒ v9에서는 updateProfile에 userInfo를 사용하므로 위 방법(강의내용)으로는 변경안됨 ⇒ 추가적인 작업 필요// App.js
// ...
const refreshUser = () => {
setUserInfo(Object.assign({}, fbAuth.currentUser));
};
// ...
⇒ Object.assign을 사용해 새로운 객체를 생성하여 React가 새로 렌더링 하도록 할 수 있음[Twitter Clone Coding] #4 FILE UPLOAD (2) | 2022.11.07 |
---|---|
[Twitter Clone Coding] #3 NWEETING (0) | 2022.11.05 |
[Twitter Clone Coding] #2 AUTHENTICATION (0) | 2022.11.03 |
[Twitter Clone Coding] #1 SETUP (0) | 2022.11.01 |