简体   繁体   中英

ReactJS - Can't obtain parameters from url using Route. How can I pass params through url with Route?

I'm trying to pass user_id through url as follows. The link http://localhost:3000/activate/1 works now but I have no idea where to find the params? If I recall correctly I should be able to access them from "props.match" but this doesn't exist. I can't find the params in props.

Can anyone point me in the right direction what I did wrong?

I'm using React 17.0.2

<Routes>
  <Route
    path={"/activate/:user_id"}
    element={
      <Activate />
    }
  />
</Routes>

Activate.js

export default function Activate(props) {

  useEffect(() => {
    console.log("props", props);
    console.log("props", props.match); // undefined
    activate();
  }, []);

  return <div>hi</div>;
}

There is a hook "useParams" in react-router-dom.

  let { id } = useParams();

where "id" is your param (users/:id) You can read about that here: https://v5.reactrouter.com/web/api/Hooks/useparams

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM