Home > database >  How to get key value from url
How to get key value from url

Time:09-29

I am using Axios to fetch data from API. From one page I am sending data through URL to another page which I want to use on another page.

http://localhost:3000/[email protected]

I want to get the value of email. In reacting project I am using react-router-dom, Axios, hooks.

CodePudding user response:

const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());

You should use like below

CodePudding user response:

One solution using hooks:

import { useLocation } from 'react-router-dom'

const location = useLocation()
const email = new URL(location.href).searchParams.get('email')

CodePudding user response:

You can use VanillaJS

const params = new URLSearchParams(window.location.search)
const email = params.urlParams.get('email');
console.log(email)

CodePudding user response:

You can use useLocation hook from react-router-dom, for example:

import { useLocation } from "react-router-dom";

const Component = () => {
  const query = new URLSearchParams(useLocation().search);

  return <div>{query.get('email')}</div>
}

It's also important to know the difference between params and queries in the routes.

  • Related