0

after i finished a cours about react i tried to create a git engine app using git api and i got this problem when i tried to catch the url param using "this.props.match.params.login" enter image description here

enter image description here

enter image description here

any help please !! thanks in advance

1 Answers1

-1

Use a functional component to get the params of a route path:

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

const YourComponent = () => {
  const { yourParam } = useParams();
  console.log(yourParam);
  // the rest of your logic
}

Edit:

According to React Router, v6 fully embraced hooks and use them to share all the router's internal state. However, in class-based components you can use a wrapper to get the params of a route path.

In your App.js, add the following snippet:

import {
  useLocation,
  useNavigate,
  useParams
} from "react-router-dom";

function withRouter(Component) {
  function ComponentWithRouterProp(props) {
    let location = useLocation();
    let navigate = useNavigate();
    let params = useParams();
    return (
      <Component
        {...props}
        router={{ location, navigate, params }}
      />
    );
  }

  return ComponentWithRouterProp;
}

const InfoRoute = withRouter(Info);

Then, in your Routes (still in App.js) use the new component:

<Route path='/allUsers/:login' element={<InfoRoute />} />

Finally, in your info.js, you can get the login param from the path url as follows:

this.props.router.params.login
  • yo sir, yes it works but i cant use it with stats i want to recover the link param in class component to be able to use states and fetch the data from api – Ahmed Lamlih Feb 16 '22 at 17:57
  • I don't think they will be able to use the useParams hook, as their `Info` component is a class component, which cannot use hooks. – Willow Feb 17 '22 at 04:26