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

768
Views
React router: useParams() returns undefined even when destructured

I am using the useParams hook in react. I have a component where I want to load the ID from 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

the useParams() console log shows the data I want in an object, but destructuring it returns undefined. I have the routes one level up in the App:

  <Link to="/">Home</Link>

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

And the Link to the FakeGame component in a Games component here:

  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>
    )
  )
}

Am I going about this correctly?

My file structure is

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

0

In this line you're declaring the name of the parameter to be "id":

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

Therefore, the destructuring returns undefined for "thisId" since there is no matching property. Either change the above line to be ..path=/:thisId.. (not recommended) or change thisId to id. Also do some reading into destructuring

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!