😀 Tweets 기능을 만들어봤으니 이제는 Profile 기능을 만들어봅니다.
💻 Profile.js
const Profile = ({userObj}) =>{
...
const getMyTweet = async() =>{
const tweets = await dbService.collection("tweets").where("creatorID","==",userObj.uid).orderBy("creatorAt").get();
tweets.docs.map(doc => doc.data());
}
useEffect(() =>{
getMyTweet();
},[])
return( ... )
}
📌 코드 설명
💻 Profile.js
const Profile = ({userObj}) =>{
const history = useHistory();
const [newDisplayName, setNewDisplayName] = useState(userObj.displayName);
...
const onChange = (e) =>{
const{
target:{value}
} = e;
setNewDisplayName(value);
}
const onSubmit = async(e) =>{
e.preventDefault();
if(userObj.displayName !==newDisplayName){
await userObj.updateProfile({
displayName: newDisplayName,
})
}
}
📌 코드 설명
🧨 But, input에 새로운 userName을 넣어주고 submit을 해줬을 때, 바로 바뀌지 않음.
💻 App.js
const refreshUser = () => {
setUserObj(autoService.currentUser)
});
}
💻 Profile.js
const onSubmit = async(e) =>{
e.preventDefault();
if(userObj.displayName !==newDisplayName){
await userObj.updateProfile({
displayName: newDisplayName,
})
};
refreshUser();
}
📌 코드 설명
refreshUser
함수를 App.js에서 선언해준 뒤, 필요한 곳으로 전파.👀Why??
- UserObject는 상당히 큰 객체.
- 우리는 이 큰 객체에서 UserName만 바꿔줄 것.
- React에서는 차이를 느끼지 못하여 reredering이 되지 않는 것.
- 그러므로 Object에서 필요한 부분만 빼온다.
💻App.js
useEffect(() =>{
authService.onAuthStateChanged((user) => {
if(user){
setIsLoggedIn(true);
setUserObject({
displayName:user.displayName,
uid:user.uid,
updateProfile: (args) => user.updateProfile(args),
});
}
...
}, []);
const refreshUser = () => {
const user = authService.currentUser;
setUserObject({
displayName:user.displayName,
uid:user.uid,
updateProfile: (args) => user.updateProfile(args),
});
}
📌 코드 설명
<참고 : 노마드 코더>