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

102
Views
react fetching and mapping data
import React,{useState, useEffect} from 'react'
import { useParams } from 'react-router-dom'
import Home from './Home'
import './detailpage.css'
function DetailPage({name,
  info,
  genre,
  _id,
  episodeNumber,
  poster}) {
  const [shows, setShows]= useState([{name:'',
  info:'',
 airingDate:'',
 _id:'',
 genre:'',
 episodeNumber:'',
  poster:''
 }])

const params= useParams();

useEffect(()=>{  
fetch("/home")
.then(res => res.json())          
.then(jsonRes => setShows(jsonRes))
 
}, [])

const b = JSON.stringify(params);

const newShows = shows.filter(a=>a._id===b) 

console.log(newShows)


return (
    <div>
<h2>.</h2>
<h2>.</h2>
<h2>.</h2>
  <h2>{JSON.stringify(params)}</h2>
 <h2>{shows.genre}</h2>
{newShows.map(a=>
  <div>
<div className='container'>
    <img className='showImg' src={a.poster} alt=''></img>
    <h2 className='showTitle'>{a.title}</h2>
    <h3>{a.genre}</h3>
    <p className='showInfo'>{a.info} </p>
    </div>

    </div>
  
  )}
<h2>{episodeNumber}</h2>
<h2>{shows.info}</h2>
    </div>
  )
}

export default DetailPage

I have tv shows on my Home page and after clicking the image I want it to load the detail page about the clicked show however I couldn't manage to do it. I tried 'filter' method in the code but it didn't work I also tried like this

const newShows = shows.filter(a=>a.genre.length>5) 

it works but this is not what I want. I would be really happy if someone could've helped. Thank you so much.

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

0

If I were you, I wouldn't use this fetch, as when you click on the image from your home you already know which tv show you want to display more details about.

I would use something like useLocation from react-router-dom, and while changing pages (home -> detail page about a tv show) carry a state variable with the specific tv show details.

https://v5.reactrouter.com/web/api/Hooks/usehistory

const handleClick = (state) => {
    history.push({ pathname: "/detail-page", state })
}

<YourTvShowImage onClick={() => handleClick(TvShowData)} />

Then on your detail page class you use something like

https://v5.reactrouter.com/web/api/Hooks/uselocation

  const location = useLocation()
  const [tvShowData, setTvShowData] = useState()
  
  useEffect(() => {
    if (location.state) {
      setTvShowData(location.state)
    }
  }, [location])
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!