简体   繁体   中英

How is path parameter name conflicts handled in react-router-dom?

home/user/:id/friends/:id
function SomeComponent() {
  const { id } = useParams();
}

Which id will be used here? There are name conflicts.

The last parameter of the path is the value that any component will see.

This is trivial to test.

Example:

const Component = () => {
  const { id } = useParams();

  return <h1>Id: {id}</h1>;
};

export default function App() {
  return (
    <div className="App">
      <Link to="/home/user/123/friends/456">Test?</Link>

      <Routes>
        <Route path="home/user/:id/friends/:id" element={<Component />} />
      </Routes>
    </div>
  );
}

编辑 how-is-path-parameter-name-conflicts-handled-in-react-router-dom

在此处输入图像描述

Don't use the same path parameter twice in the same path string per route.

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