Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useLocation for query params

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 :: sequelize findall 
Javascript :: best javascript books 
Javascript :: isupper 
Javascript :: angularjs show form validation errors 
Javascript :: javascript prevent value change in select option 
Javascript :: Run Express in Production Mode 
Javascript :: what does event emitter do in angular 
Javascript :: angular ngstyle variable 
Javascript :: js split string 
Javascript :: how to update state.item[1] in state using setState? React 
Javascript :: groubbykey js 
Javascript :: set up emet for jsx in vs code 
Javascript :: gojs update text 
Javascript :: round down javascript 
Javascript :: javascript loop last index 
Javascript :: vuejs pass all events to child 
Javascript :: check if computer online js 
Javascript :: auto increase hight of textarea with alpine js 
Javascript :: javascript get the screen color depth 
Javascript :: window onfocus 
Javascript :: mongooseautoincrement 
Javascript :: set radgrid datasource clientside 
Javascript :: cypress element length 
Javascript :: async/await 
Javascript :: ejs js 
Javascript :: html js hide or show iframe 
Javascript :: appinsights trackException javascript 
Javascript :: how to hide api key in react app 
Javascript :: pdf.js get current page number 
Javascript :: nodejs express routing get 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =