Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!