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

776
Views
Reaccionar enrutador: useParams () devuelve indefinido incluso cuando está desestructurado

Estoy usando el gancho useParams en reaccionar. Tengo un componente donde quiero cargar la ID desde la URL.

 import React from 'react'; import { useParams } from 'react-router-dom' const FakeGame = () => { let { thisId } = useParams(); console.log(thisId); console.log(useParams()); return ( <h1>This ID is: {thisId}</h1> ) } export default FakeGame

el registro de la consola useParams() muestra los datos que quiero en un objeto, pero la desestructuración devuelve indefinidos. Tengo las rutas un nivel más arriba en la App:

 <Link to="/">Home</Link> <Routes> <Route path="/:id" element={<FakeGame currentId={currentId}/>}></Route> <Route path="/" element={<Home currentId={currentId} setCurrentId={setCurrentId}/>} /> </Routes>

Y el enlace al componente FakeGame en un componente de juegos aquí:

 const Games = ({ setCurrentId }) => { const games = useSelector((state) => state.games); return ( !games.length ? <CircularProgress /> : ( <Grid className={mergeClasses.container} container alignItems="stretch" spacing={3}> {games.map((game) => ( <Grid key={game._id} item xs={12} sm={6}> <Link to={`/${game._id}`}>FAKE GAME</Link> <Game game={game} setCurrentId={setCurrentId}/> </Grid> ))} </Grid> ) ) }

¿Estoy haciendo esto correctamente?

Mi estructura de archivos es

 - App.js + components - FakeGame.js + Games - Games.js
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En esta línea, está declarando que el nombre del parámetro es "id":

 <Route path="/:id" element={<FakeGame currentId={currentId}/>}></Route>

Por lo tanto, la desestructuración devuelve undefined para "thisId" ya que no existe una propiedad coincidente. Cambie la línea anterior para que sea .. path=/:thisId .. (no recomendado) o cambie thisId a id . También lea un poco sobre la desestructuración

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!