0

I am trying to receive the 'id' that is in the URI. But I am unable to collect it.

Route:

 <Route path="/reports/:id" >
       {!isLoggedIn ?
          <Redirect to="/login" /> : <UserReport userData={userData} />
       }
 </Route>

Link:

<Link to={'/reports/'+employee.id}>
    <span className="btn btn-sm btn-primary" style={{cursor:'pointer'}}>
          Report <FontAwesomeIcon icon="history" />
    </span>
</Link>

Component:

const UserReport = (props)=>{

    return (
        <div>
            {props.match.params.id}
        </div>
    );
}

Error:

It says props.match is undefined

But when I try the component attribute in Route, it works

 <Route path="/reports/:id" component={UserReport} >

Is it possible to do this the way I am trying? Please help..This is my first React project.

2
  • Check this, maybe this component can solve your problem reactrouter.com/web/api/withRouter Commented Mar 9, 2021 at 9:16
  • 1
    @Chaka15 thank you for the suggestion, but Mohiuddin's answer saved my day! Commented Mar 9, 2021 at 9:22

1 Answer 1

1

you can use useParams method from react-router-dom

import { useParams } from "react-router-dom";
const UserReport = () => {
    const { id } = useParams();
    return (
        <div>
            {id}
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.