Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda