Home > OS >  How do I add id parameters to the url of a html page?
How do I add id parameters to the url of a html page?

Time:09-19

I want to add URL parameters to a HTML page. Can't seem to find out how to do it using react without the use of the Link class.

I want the id so that I can use to pass to the db and populate the page(anotherpage.html) with the correct information.

I'm looking to do the following

ex: https://www.example.com/anotherpage.html/:id

id = some-sub-page

CodePudding user response:

If I understand correctly, you could do it the same as I did

export const fetchData = async (newFetchOffset) => {
// console.log(fetchOffset);
const res = await axios(`https://xoosha.com/ws/1/test.php?offset=${newFetchOffset}`)
return res.data

}

the Repository is available at THIS IS A LINK

CodePudding user response:

Add route definition for sub page

<Route path="/subpage/:id">
  <SubPageComponent />
</Route>

Now in sub page component:

import { useParams } from 'react-router';

export default function SubPageComponent() {
  const { id } = useParams();
}

Also, why do you have .html in route, please read the docs of react and any routing library: https://v5.reactrouter.com/web/example/url-params

For non react router way:

window.location.href.split('/').reverse()[0]
  • Related