Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
I want to call function received from context in hook useEffect

I am trying to call function received from context in hook useEffect.

I am getting function (searchUser) and obj (user) from context and then I am trying to call this function (searchUser) in useEffect and due to calling this function I get data in user obj from context.

Every thing is ok, till I am not trying to output data in return like that: {user.login}

Function searchUser written in context file:

const [user, setUser] = useState(null)
    
function searchUser(login){

    fetch(`https://api.github.com/users/${login}`)

    .then((response) => {
        if(response.ok){
            console.log(response)
            return response.json()
        }
    })
    .then((data) => {
        setUser(data)
    })
    .catch((error) => {
        console.error(error)
    })
}

File User.jsx, where I trying to output the user.login

import React, {useEffect, useContext} from "react"
import {useParams} from "react-router-dom"
import GithubContext from "../context/GithubContext.jsx"

function User(){
    const {login} = useParams()
    const {user, searchUser} = useContext(GithubContext)

    useEffect(() => {
        searchUser(login)
    }, [])

    return(
        <div>{user.login}</div>
    )
}

export default User;

IDE console output:

IDE console output screenshot

Even when I am using

// eslint-disable-next-line react-hooks/exhaustive-deps

It doesn't help

Browser console output:

Browser console output

I would be very grateful for help

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda