So I'm new to React and JS in general and have been following a course online. The thing is that, after a few chapters, I noticed that some stuff routes-related that the guy does do not work anymore. For example:
<Route path='/something' component={Something}/>
I spent the whole morning searching online. It seems that he uses a different version of react-router-dom than me. So I looked up how to do that same thing in newer versions from their official docs and eventually solved it by implementing routes like:
<Route path='/something' element={<Something/>} />
Now I have another problem. I want to fetch a parameter from the URL but it does not work either and the official docs only offer an example using functions (but I kinda need to use classes).
This is how I currently implement it:
<Route path='/profile/:user' element={<UserInfo/>}/>
UserInfo.js:
import React from 'react';
class UserInfo extends React.Component {
state = { user: '' }
componentDidMount() {
console.log(this)
let user = this.props.match.params.user;
this.setState({user:user});
}
render() {
const { user } = this.state
return(
<div>
<h1>{user}</h1>
</div>
)
}
}
export default Card
But it does not work either because this.props is empty "props: Object { }". There should be a match object but there is nothing inside (according to the dev console). How can I solve this without making UserInfo a function? Because I want to have states.