0

I'm trying to fetch 2 API's fetchTrending (first), getTrailer (second) in order.

    // Trending Data
    const [content, setContent] = useState([])

    // Trailer Data
    const [trailerLink, setTrailerLink] = useState([])

    // Loading State
    const [loading, setLoading] = useState(true)

    // Fetching Trending Data (content)
    const fetchTrending = async () => {
        const { data } = await axios.get(`${TRENDING}${process.env.REACT_APP_API_KEY}`);
        setContent(data.results);
        console.log(content[0])
        getTrailer(content[0]?.media_type, content[0]?.id)
    }

    // Fetching Trailer Data (trailerLink)
    const getTrailer = async (media_type, id) => {
        const { data } = await axios.get(`${baseURL}/${media_type}/${id}/videos?api_key=${process.env.REACT_APP_API_KEY}&language=en-US`)
        const filteredData = data.results.filter(search => search.type === "Trailer")
        setTrailerLink(filteredData[0])
        console.log(media_type, id)
        console.log(trailerLink)
    }

    useEffect(() => {

        fetchTrending()
        setTimeout(() => setLoading(false), 3000)
    }, [])

Once the page loads, I'm getting trailerLink value as undefined.

If I make any changes in the jsx file and click save, trailerLink updates to it's right value.

But if I reload the page, same issue occurs.

Phil
  • 157,677
  • 23
  • 242
  • 245
Akhil
  • 49
  • 6
  • Stop using `console.log()` to verify your application state – Phil Oct 25 '22 at 02:26
  • I'm new to JS, can you suggest any other methods to check the data? – Akhil Oct 25 '22 at 02:27
  • 1
    1) Just use the data in your app. If it's not what you expect, that's when you start debugging, not before. 2) Use your browser's built-in dev-tools debugger. 3) Use specific debugging tools like the React Dev Tools browser extension – Phil Oct 25 '22 at 02:34

0 Answers0