Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react-router-dom useLocation

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

function Locaions() {

  let location = useLocation();

  return (
    <>
    {location.pathname === "/Home" ?
    "Hi i am at the homepage"
    :
    "d-none"
    }
    </>
  );
}
Comment

useLocation

// React Router v6: hook returns current location object
import { useLocation } from 'react-router-dom'

function App() {
  let location = useLocation()
  
  useEffect(() => {
    ga('send', 'pageview')
  }, [location])
  
  return (/*...*/)
}
Comment

useLocation

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

react useLocation object

{
    key: 'ac3df4',
    pathname: '/somewhere',
    search: '?some=search-string',
    hash: '#howdy',
    state: {
      [userDefined]: true
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: get home dir in nodejs 
Javascript :: laravel link custom javascript file 
Javascript :: email regex pattern input css 
Javascript :: object json parse nestjs 
Javascript :: axios how to get error response 
Javascript :: bootstrap programmatically change tab 
Javascript :: timeout httppost angular 
Javascript :: axios send file 
Javascript :: if else dart 
Javascript :: Update multiple documents by id set. Mongoose 
Javascript :: object traversal javascript 
Javascript :: number_format in jquery 
Javascript :: js click anchor 
Javascript :: jquery declare variable 
Javascript :: file upload in jquery 
Javascript :: string concatenation in js 
Javascript :: check user by id discord js 
Javascript :: jquery nice select 
Javascript :: create javascript array 
Javascript :: Bots member count discord js 
Javascript :: fromcharcode in javascript 
Javascript :: javascript generate random number 
Javascript :: how to get the computer date and time jquery 
Javascript :: encode jwt token javascript 
Javascript :: Easy REACT download image 
Javascript :: dom element set id 
Javascript :: js add text after div 
Javascript :: axios fetch 
Javascript :: js array includes 
Javascript :: how to play background sound js 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =