Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript urlsearchparams to string

let params = new URLSearchParams('foo=2'); // "location.search" to read from URL

params.append('bar', 4);                   //Add a bar parameter.
console.log(params.toString());            //Prints 'foo=2&bar=4'

location.search = params.toString();       // write changes to URL
Comment

URLSearchParams

import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";

function CheckoutDetails() {
  const location = useLocation();
  const [amountValue, setAmountValue] = useState(1);

  // function to get query params using URLSearchParams
  useEffect(() => {
    const searchParams = new URLSearchParams(location.search);
    if (searchParams.has("amount")) {
      const amount = searchParams.get("amount");
      setAmountValue(parseInt(amount, 10));
    } else {
      setAmountValue(1);
    }
  }, [location]);

  return (
  	<p>Amount: {amountValue}</p>
  )
  
Comment

PREVIOUS NEXT
Code Example
Javascript :: multipline and single line regex pattern 
Javascript :: json objects 
Javascript :: Accessing $route.params in VueJS 
Javascript :: days array in js 
Javascript :: join a list of strings into one string javascript 
Javascript :: how to change the color of a console.log in javascript 
Javascript :: react-native razorpay 
Javascript :: js string reverse exception 
Javascript :: jest expect not contain 
Javascript :: wordpress disable jquery migrate 
Javascript :: js arithmetic operators 
Javascript :: debounce js 
Javascript :: npm react copy to clipboard 
Javascript :: ruby hash to json 
Javascript :: add regexp to antd 
Javascript :: quasar apexchart 
Javascript :: google auth.onstatechange 
Javascript :: remove repeated characters from a string in javascript 
Javascript :: javascript fs read 
Javascript :: get bottom position of element javascript 
Javascript :: flatten array recursively 
Javascript :: for in react 
Javascript :: react select npm 
Javascript :: javascript object.assign 
Javascript :: javascript multidimensional array foreach 
Javascript :: string concatenation javascript 
Javascript :: node js check type of variable 
Javascript :: sequelize sqlite example 
Javascript :: install javascript kali linux 
Javascript :: How To Open Phone Dialer and Make Call From React Native App 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =