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

84
Views
How to properly fetch data on user click?

I'm trying to display a menu with three tags Streaming,On Tv and In Theaters similar to what you can find on https://www.themoviedb.org/

The default data that is displayed is the Streaming section, which is receiving JSON from getStaticProps. I only want to fetch On Tv and In Theaters endpoints if the user clicks on it, and later cache that information. This is my code so far (only On Tv example, though In theaters would be the same):

import useSWR from 'swr'
import React, { useState } from "react";

const fetcher = async (e) => {
    const res = await fetch(e);
    const data = await res.json();
    return data
};
  
const Trending = ({data}) => {
    const [media, setMedia] = useState(data); //data used to display shows/movies
    const [onTv, setOnTv] = useState(null);
    
    const [shouldFetchTv, setShouldFetchTv] = useState(false);
    const urls = [
        `https://api.themoviedb.org/3/tv/on_the_air?api_key=${process.env.NEXT_PUBLIC_API_KEY}&language=en-US&page=1`,
        `https://api.themoviedb.org/3/movie/now_playing?api_key=${process.env.NEXT_PUBLIC_API_KEY}&language=en-US&page=1&region=US`
    ]

    const tv = useSWR(shouldFetchTv && urls[0], fetcher);
    const tvResult = tv.data;
    
    const handleTv = () =>  {
        onTv ? setMedia(onTv) //if onTv has data then just set it
        :
        setShouldFetchTv(true); //fetches endpoint on users click
        setOnTv(tvResult); // caches the data
        setMedia(onTv); //sets media object with the cached data
    }

   (...Some more code)
       <div onClick={handleTv}>  
          <h1>On Tv</h1>
          </div>

The problem is that onTv object doesn't get properly defined on the first click, but on the third. I've been trying to implement useEffect to no effect (no pun intended), but I'm still learning hooks so maybe I'm missing something. How can I get it to work properly?

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

0

On first click shouldFetchTv is false and onTv is null. shouldFetchTv sets to true

On second click shouldFetchTv && urls[0] becomes true and async request is sent, tvResult = tv.data is null because it async

On third click you send second request and result of first is available so it sets onTv

On fourth click onTv is not null

Because useSWR is using browser cache async result is available on third click

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!