Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda