'Cannot read properties of undefined (reading 'params').trying to get params from url params

I am trying to get the URL params inside a Card class component in order to display it:

import React from "react";

class  Card extends React.Component {

    state = { user :''}

    componentDidMount(){
        let user = this.props.match.params.user
        this.setState({ user : user})
    }


    render() {
        const { user } = this.state

        return(
            <div >
            <h3 className="ui header">{user}</h3>


        </div>
        )
    }
}

the App.js file

import React from "react";
import { BrowserRouter, Route, Routes, Outlet  } from "react-router-dom";
import Navbar from "./componenets/NavBar";
import Home from "./componenets/Home";
import Contact from "./componenets/Contact";
import About from "./componenets/About";
import Card from "./componenets/Card";

function App() {
  return (
    <BrowserRouter>
      <div className="App">
        <Navbar />
        <Routes>
          <Route path="/" element={<Home/>} />
          <Route path="/about" element={<About/>} />
          <Route path="/contact" element={<Contact/>} />
          <Route exact path="card/:user" element ={<Card/>} />
        </Routes>
      </div>
    </BrowserRouter>
  );
}
export default App;

and the index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';


ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
)

and this is the package.json

{
  "name": "react-router",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^6.2.1",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

so when I start the react app and try to go the path http://localhost:3000/card/choosenName I expect the 'chosenName' to paper in the card instead I get this error in the Browserconsole

Uncaught TypeError: Cannot read properties of undefined (reading 'params')
    at Card.componentDidMount (Card.js:9:1)
    at commitLifeCycles (react-dom.development.js:20663:1)
    at commitLayoutEffects (react-dom.development.js:23426:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
    at invokeGuardedCallback (react-dom.development.js:4056:1)
    at commitRootImpl (react-dom.development.js:23151:1)
    at unstable_runWithPriority (scheduler.development.js:468:1)
    at runWithPriority$1 (react-dom.development.js:11276:1)
    at commitRoot (react-dom.development.js:22990:1)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source