I am doing a clone of Netflix using React.js for learning React.js. I integrated an API from TMDb and for the case of banner image , name , and description , I have an array of 20 Objects which contains all details of 20 movies , and when the page is refreshed , I need to change the Banner into another random movie in the array. I don't Know how to select a random object by its number. Please Help Me To do this.
Here Is My Code :
import React, { useState } from 'react'
import './Banner.css'
import {useEffect} from 'react'
import {API_KEY,imageUrl} from '../../Constants/Constants'
import axios from '../../Axios'
function Banner() {
const [movie, setMovie] = useState()
useEffect(() => {
axios.get(`trending/all/week?api_key=${API_KEY}&language=en-US`).then((response)=>{
console.log(response.data);
setMovie(response.data.results[11])
})
}, [])
return (
<div className="banner" style={{backgroundImage:`url(${movie ? imageUrl+movie.backdrop_path : ""})`}}>
<div className="content">
<h1 className="title">{movie ? movie.title : ""}</h1>
<div className="banner-buttons">
<button className="button">Play</button>
<button className="button">My List</button>
</div>
<h1 className="description">{movie ? movie.overview : ""}</h1>
</div>
<div className="fade-bottom"></div>
</div>
)
}
export default Banner