0

I have user profile component in my reactJS App and I want to display user profile picture on load of component I'm using nodeJS Express as BE and PostgreSQL as DB. Here is what I'm trying to do

const [img,setImg] = useState()
let user : any = useSelector((state : any) => state.singleUser)
let {avatar} = user


 useEffect((): void => {
      axios
    .get(`http://localhost:4000/getUser/${match.params.userId}`)
    .then((users) => {
      dispatch(actions.getUser(users.data))
    })
    .catch((err) => {
      console.log('--------err', err);
    })
},[])

After this i setImg to the img like this :

useEffect (() : void => {
setImg(avatar)
console.log('--------img', img);
  })

When i console.log(img) i get img path like this uploads/avatar-1594717135245.jpeg.gif

Here is my img tag : <img ref={fileRef} src={img} width={'400'} alt={'picture'}/> it is showing no errors in the console but the problem is it is displaying img for a second and then it dissapears.Any suggestions why and how can i fix it ?

Nika Roffy
  • 891
  • 2
  • 8
  • 20

1 Answers1

-1

Try moving the setImage(users.data.avatar) directly after the dispatch, you already have the avatar information there. Remove the seconds useEffect, you'll have less moving parts to debug the problem.

Julio Truzzi
  • 512
  • 1
  • 4
  • 11