μΌλ¨, κ°λ΅νκ² λ§νμλ©΄ useEffect μμ μ€νλ ν¨μμ λ³μκ°μ useEffect μμ‘΄μ±λ°°μ΄μ μ€μ μ μν΄μ£Όμ΄μ μκΈ΄ λ¬Έμ μ΄λ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μν λ°©λ²μ΄ λκ°μ§ μ‘΄μ¬νλ€.
useEffect μμ ν¨μλ₯Ό μ μΈν΄μ£Όκ³ , κ·Έ ν¨μμ λ°μν λ³μκ°μ useEffect μμ‘΄μ± λ°°μ΄μμ μ€μ ν΄λκΈ°
useEffect(() => {
const getChatroomData = () => {
axios({
url: `https://mandarin.api.weniv.co.kr/post/${chatRoomId}`,
method: 'get',
headers: {
Authorization: `Bearer ${userToken}`,
'Content-type': 'application/json',
},
})
.then((response) => {
setChatRoomData(response.data.post);
})
.catch((error) => {
console.log(error);
});
};
if(chatRoomId&&userToken){
getChatroomData();
}
}, [chatRoomId,userToken]);
μ£Όμμ ν΅ν΄ μμΈμ²λ¦¬νκΈ° ( κ·Όλ³Έμ μΈ ν΄κ²°λ°©λ²μ μλ )
β κ·ΈλΌμλ μ¬μ©νλ μ΄μ : μ¬μ¬μ©λ ν΄μΌνκ³ , λ³μκ° μλ ν¨μλ₯Ό useEffectλ₯Ό ν΅ν΄ λ λλ₯Ό ν΄μΌν κ²½μ°μλ μ΄μ© μ μμ΄ μ£Όμμ²λ¦¬λ₯Ό νμ¬ μ¬μ©νλ€.
const getChatroomData = () => {
axios({
url: `https://mandarin.api.weniv.co.kr/post/${chatRoomId}`,
method: 'get',
headers: {
Authorization: `Bearer ${userToken}`,
'Content-type': 'application/json',
},
})
.then((response) => {
setChatRoomData(response.data.post);
})
.catch((error) => {
console.log(error);
});
};
useEffect(() => {
if(chatRoomId&&userToken){
getChatroomData();
}
// μλμ κ°μ μ£Όμμ²λ¦¬λ₯Ό ν΅ν΄, eslint warningμ΄ λ°μνμ§ μλλ‘ μ€μ νλ€.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chatRoomId,userToken]);
ν νλ‘μ νΈ μ€, 1μ λ°©μμ νν μ μμμλ 2λ₯Ό μ΄ κ²½μ°λ 리ν©ν λ§ κ³Όμ μ κ±°μΉ μμ μ΄λ€.