Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide query params from the URL while using router.push?

I'm passing some data from one page to another via query params but I want to hide the query from the URL.

Here is my code.

import { useRouter } from "next/router";

const handleFormSubmit = async (values) => {
    const res = await AuthService.login(values.email, values.password);

    if (res.status === 200) {
   
       if (res.data.is_phone_number_exists === 1 && res.data.is_phone_number_verified === 0) {
        router.push({
          pathname: '/otp-verification',
          query: { email: values.email, password: values.password, phone_number: res.data.phone_number} //I want hide this query part from the url
        }) 
      }
    }
  }

How to hide the query params from the URL while using the router.push()? Or is there any other way for doing the same thing?

like image 933
Ishrat Avatar asked Dec 21 '25 08:12

Ishrat


2 Answers

When using next/router, you can pass query parameters without being visible on the address bar using the second argument as in the router.push call.

router.push({
    pathname: '/otp-verification',
    query: { email: values.email, password: values.password, phone_number: res.data.phone_number }
}, '/otp-verification')
like image 150
juliomalves Avatar answered Dec 23 '25 01:12

juliomalves


You can use as props with next/link as well, to hide query parameters. for Example:

<Link href={`/blogs/${item.slug}?id=${item.id}`} as={`/blogs/${item.slug}`} passHref>
    <a href="" className="hover:text-cyanBlue">
        Read More
    </a>
</Link>

Bear in mind, this won't work if you set target="_blank" to anchor tag or open link to next tab of browser.

like image 43
Anurag Tripathi Avatar answered Dec 23 '25 01:12

Anurag Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!