1
const userdata = [
{
    id:1,
    name:"kuber",
    phone:"8989"
},
{
    id:2,
    name:"sahil",
    phone:"9696"
}

];
export default userdata

From this to>>> <Route path={"user/:id"} component={user} />

What code should i use in "User" component if useParams has id=1 then how to show only id 1, name kuber, phone 8989

2
  • if forward object in path when path not friendly and And it is not suitable for SEO Commented Sep 27, 2020 at 8:35
  • you should this answer multiple-params-with-react-router Commented Sep 27, 2020 at 8:43

1 Answer 1

3

In the simplest way you can do it like this:

in your User.js/jsx import the userData. Then you can get the id from url using props.match.params.id and then you can filter from userData from that props.match.params.id . Note: You need to do this inside so every time id changes it gets the user of that id

useEffect(()=>{

 // your code here
 
},[props.match]);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for answering this using filter only return one value can you help me out what exactly I code to follow to achieve all elements in id = 1
if you let user = userData.filter(user => user.id === props.match.params.id); The user object contains all the data for user with id 1
My bro work well thanks a lot for this dynamic routing works useEffect(()=>{ let user = userdata.filter(user => user.id == id); setnam(user) console.log(id) console.log(user) },[id]) return( <> <div className="p-3"> <h2>Description</h2> {nam.map(myuser=>( <li>{myuser.name}</li> ))} </div> </> )

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.