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

162
Views
Reactjs Empty State when Page Load

API: https://developers.themoviedb.org/

const [searchQuery, setSearchQuery] = useState();
const [searchResults, setsearchResults] = useState([]);

    const getSearchResults = () => {
      baseService.get(`/search/multi?api_key=${API_KEY}&language=en-US&query=${searchQuery}`)
      .then(data=>setsearchResults(data.results))
  }

  useEffect(() => {
    getSearchResults()
    console.log(searchResults)
  }, [searchQuery])
return ( 
    <Container>
            <TextField  color="primary" label="Search for anything" size="small" onChange={(e) => setSearchQuery(e.target.value)}/>
              {searchResults && searchResults.map((search,key) => (
                <span key={key}>{search?.title}</span>
              ))}
            </Container>

baseservice.js is like that

import { API_URL } from "./config"
export const baseService = {
    get: async (url) => {
        let response = [];
        await fetch(API_URL+url, {
        })
            .then((res) => res.json())
            .then((data) => {
                response = data
            })
        return response;
    }
  }

1.Picture is when page load. enter image description here

2.Picure is when entry search term. After the entry search:

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In baseService.get you’re returning response before the promise has resolved, it can be simplified to this:

export const baseService = {
    get: (url) => {
        return fetch(API_URL+url)
          .then((res) => res.json())
    }
  }
about 4 years ago · Juan Pablo Isaza Report

0

This happens because you're logging searchResults inside a useEffect that has a searchQuery dependency, so it logs the first time state are set and then it doesn't log when it changes from fetch response. If you wanna log it correctly you have to add searchResults as a dependecy.

  useEffect(() => {
    getSearchResults()
    console.log(searchResults)
  }, [searchQuery, searchResults])
about 4 years ago · Juan Pablo Isaza Report
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!