Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react js usehistory push and pass props

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

const FirstPage = props => {
    let history = useHistory();

    const someEventHandler = event => {
       history.push({
           pathname: '/secondpage',
           search: '?query=abc',
           state: { detail: 'some_value' }
       });
    };

};

export default FirstPage;


Accessing the passed parameter using useLocation from 'react-router-dom':

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

const SecondPage = props => {
    const location = useLocation();

    useEffect(() => {
       console.log(location.pathname); // result: '/secondpage'
       console.log(location.search); // result: '?query=abc'
       console.log(location.state.detail); // result: 'some_value'
    }, [location]);

};
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery set attribute stack overflow 
Javascript :: javascript find shortest word in string 
Javascript :: react fetch post 
Javascript :: jquery remove br from div 
Javascript :: datatables change width of columns 
Javascript :: put form action jquery 
Javascript :: remove last character javascript 
Javascript :: redirect in netlify react 
Javascript :: typescript read url parameters 
Javascript :: each input form jquery 
Javascript :: nodejs get file size 
Javascript :: css customize console.log 
Javascript :: ngfor object 
Javascript :: iframe in react native 
Javascript :: node js fetch 
Javascript :: jquery delete request 
Javascript :: setpresence discord.js 
Javascript :: document on click dynamic element 
Javascript :: js response to json log 
Javascript :: cypress set timeout for locator 
Javascript :: javascript get string between two characters 
Javascript :: jquery insert after 
Javascript :: console log add new line 
Javascript :: javascript foreach get key and value 
Javascript :: Console.log CSS styling 
Javascript :: add style javascript 
Javascript :: numeros primos js 
Javascript :: js regex domain name 
Javascript :: confirm delete message in jquery 
Javascript :: find min value in array javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =