6

I want to get the id value from this React Router:

<BrowserRouter basename="/api">
    <Switch>
        <Route path="/pm" exact component={PaymentMethod}/>
        <Route path="/pw" exact component={PaymentWeb}/>
        <Route path="/rg" exact component={Registrasi}/>
        <Route path="/bonus/:id" exact component={BonusScreen}/>
        <Route path="/hb" exact component={HistoryBonus}/>
    </Switch>
</BrowserRouter>

in my BonusScreen I tried to print the value by using this :

const userId = this.props.match.id;
console.log(this.userId);

from the browser I try to access the URL like this:

bonus/NGO628567652201

But it always returned undefined.
How can I fix this ?

kurniawan26
  • 793
  • 4
  • 16
  • 35
  • Possible Duplicate of [get-path-params-in-react-router-v4](https://stackoverflow.com/questions/45468837/get-path-params-in-react-router-v4/45469647#45469647) – Shubham Khatri Apr 05 '18 at 09:15
  • You tagged this [tag:api] but that says "DO NOT USE: Tag with the library you mean, [api-design], or something else appropriate instead.". You should fix that. – Quentin Apr 05 '18 at 09:42

2 Answers2

21

In case of anyone having this problem. The correct would be:

const userId = this.props.match.params.id;

05/2020 UPDATE USING REACT ROUTER V4 AND HOOKS

import { useParams } from 'react-router-dom';

//Then inside your component
const { id } = useParams();
Alcides Bezerra
  • 417
  • 4
  • 11
1

You can try this

const id = this.props.computedMatch.params.id

Shan
  • 613
  • 9
  • 21