📌  相关文章
📜  javascript 从 URL 获取查询参数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:22.504000             🧑  作者: Mango

代码示例5
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 (
      

Amount: {amountValue}

)