Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

props.history.push with data



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;
/*
Extending the solution (suggested by Shubham Khatri) for use with React hooks (16.8 onwards):

package.json (always worth updating to latest packages)

{
     ...

     "react": "^16.12.0",
     "react-router-dom": "^5.1.2",

     ...
}
*/
//Passing parameters with history push:

//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

passing data in react router history,push

this.props.history.push({
  pathname: '/template',
  search: '?query=abc',
  state: { detail: response.data }
})
Comment

this.props.history.location.push

props.history.push({  pathname: '/register', state: data_you_need_to_pass});
Comment

passing data in react router history,push

<Link to={{
      pathname: '/template',
      search: '?query=abc',
      state: { detail: response.data }
    }}> My Link </Link>
Comment

passing data in react router history,push

this.props.location.state.detail
Comment

this.props.history.location.push

<Link to={{  pathname: "/register",  state: data_you_need_to_pass }}> Register</Link>
Comment

PREVIOUS NEXT
Code Example
Javascript :: Print a number with commas as thousands separators in JavaScript 
Javascript :: javascript loop through object values 
Javascript :: js canvas line end 
Javascript :: react-native loading screen 
Javascript :: jquery get data-id 
Javascript :: nombre random js 
Javascript :: two button in one row react native 
Javascript :: javascript emit beep 
Javascript :: javascript trim each element in array 
Javascript :: jquery each data 
Javascript :: how to check if object is empty javascript 
Javascript :: js reduce concat numbers 
Javascript :: js before unload 
Javascript :: js promise all return json array 
Javascript :: reinstall node modules packages 
Javascript :: javascript date set time 23 59 59 
Javascript :: missing from-clause entry for table sequelize limit 
Javascript :: Get Substring between two characters using javascript 
Javascript :: javascript init an array to 0 
Javascript :: randomColor 
Javascript :: generate random random number with fixed length 
Javascript :: DNA Pairing solution 
Javascript :: angular create component without test 
Javascript :: .on change get value 
Javascript :: LazyLoad for images, Videos and Iframes JavaScript and JQuery 
Javascript :: uuid for react native 
Javascript :: check if date is after or before with moment 
Javascript :: js rectangle collision 
Javascript :: react/ionic ion-app undefined 
Javascript :: react native monorepo module resolver outside app 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =