I'm trying to create a useState hook that contains an array of a user's friends. To access this friend list, I'm grabbing the array from the firebase database. However, when I try to update the useState, it still logs friendList as undefined. If I log the values, it tries to add them and appears fine.
function SideBarFriends (props: any) {
const { sbView, setSbView } = useContext(ViewContext);
const [friendList, setFriendList] = useState<string[]>();
useEffect(() => {
getFriends()
}, [])
function getFriends() {
setFriendList(null);
const query = userRef.where('uid', '==', auth.currentUser.uid);
query.get().then((snapshot) => {
snapshot.forEach((doc) => {
const { friends } = doc.data();
friends.map((f: string) => updateFriends);
})
})
function updateFriends(item: any) {
if (!friendList) {
setFriendList([item]);
return;
}
else {
setFriendList(friendList => [...friendList, item])
return;
}
}
}
return(
<section>
<FontAwesomeIcon className='text-white/70 pl-4' icon={faMagnifyingGlass} onClick={() => {setSbView(false)} } />
</section>
)
}
I'm struggling for ideas here as I did a similar function for my search user function, which works with no issues.