0

I checked my code more than 4 times and rewrited entire code. but I couldn't find any difference between Youtube code and mine.

here is the code

import axios from 'axios';
import { useEffect, useState } from 'react';
import './conversation.css';

export default function Conversation({conversation,currentUser}) {
    const [user,setUser] = useState(null);
    
    useEffect(()=>{
        const friendId = conversation.members.find((m)=>m !==currentUser._id);
        
        const getUser = async ()=>{
            try{
                const res = await axios ('/users?userId='+friendId);               
                setUser(res.data);                
            }catch(err){
                console.log(err);
            }            
        };        
        getUser()               
    },[currentUser, conversation]);    

    return(
       <div className='conversation'>
        <img 
            className='conversationImg' 
            src='https://img.seoul.co.kr/img/upload/2020/05/25/SSI_20200525111945_O2.jpg'
            alt=''
        />
        <span className='conversationName'>{user.username}</span>
       </div>
    );
}

enter image description here

enter image description here

console messages

it works in Youtube practice.

Actually, I found this when i changed this

const [user,setUser] = useState(null); -> const [user,setUser] = useState('');

it worked

or

const [user,setUser] = useState(null); leave it like practice code instead I changed

setUser(res.data); -> setUser(res.data.username);
<span className='conversationName'>{user.username}</span>; ->
<span className='conversationName'>{user}</span>;

I solves problems can move on next but I couldn't get it. Please explain me why origin code has caused the error.

thebat
  • 21
  • Your `user` state starts off as `null`, so you're going to be doing `null.username` in your JSX so you get an error. The first time your `useEffect` runs is after the initial render, and it still takes some time for your asynchronous HTTP request to complete and provide you with a response. It's only once you get the response will `user` not be `null` anymore which is when its safe to use `user.username` – Nick Parsons Jun 19 '22 at 11:24
  • I really appreciate your answer. But I'm still curious. That code and mine are exactly the same, but only my code is giving the error. – thebat Jun 19 '22 at 12:17
  • can you share the code that you are copying from? There must be a difference... or perhaps there is a mistake in the video – Nick Parsons Jun 19 '22 at 12:29
  • Above code is mine. here is Youtube practice link. https://www.youtube.com/watch?v=HggSXt1Hzfk that part starts from 57:40. In my eyes.. I don't have any typing mistakes. – thebat Jun 19 '22 at 13:08
  • hm, interesting. In my eyes that code should be crashing, unless react does something with caching state for hot-reloads while you're developing. I can see in the actual github repo on that vid the source code is using optional chaining (`?.`) before reading the `.username` property - which you can see [here](https://github.com/safak/youtube/blob/chat-app/client/src/components/conversations/Conversation.jsx). Maybe he fixes it later on in the video (just checked, he fixes it at [1:03:10](https://youtu.be/HggSXt1Hzfk?t=3790)) – Nick Parsons Jun 19 '22 at 13:16

0 Answers0