So I have some logic I want to run on state change, but not on mount. The default state value is an empty object.(currentTarget is the state). So I figured an if statement would do the trick.
const fetchChat = async () => {
const getChatData = await fetch('http://localhost:3001/getChat')
const chatData = await getChatData.json()
const checkForChat = chatData.some((chat) => {
return chat.psuedoID.includes(userInfo._id) && chat.psuedoID.includes(currentTarget._id)
})
console.log(checkForChat)
if (checkForChat === false) {
createChat([userInfo._id, currentTarget._id])
}
}
useEffect(() => {
if (currentTarget !== {}) {
console.log(currentTarget)
fetchChat()
}
}, [currentTarget])
Despite the console log showing that the state hasn't changed and is still an empty object, the logic within the if statement is still running on mount.