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

react router history push parameter

this.props.history.push({
  pathname: '/template',
  search: '?query=abc',
  state: { detail: response.data }
})
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

PREVIOUS NEXT
Code Example
Javascript :: get current scroll height javascript 
Javascript :: react function with form 
Javascript :: image border shadow react native 
Javascript :: angular build with configuration 
Javascript :: refresh page after success ajax 
Javascript :: npm install router dom 
Javascript :: find the words separated by whitespace in a string javascript 
Javascript :: load script js 
Javascript :: serve a file in express 
Javascript :: javascript remove item from array in loop 
Javascript :: javascript regex for usernames 
Javascript :: jsconfig alias 
Javascript :: jqery slectt div in div 
Javascript :: angular bootstrap not working 
Javascript :: react-native-youtube-iframe android crash 
Javascript :: jschlatt 
Javascript :: Material App debug mode 
Javascript :: Odoo13 How to open a JSON file and read it Avatar arian_shariat@comp.iust.ac.ir 23 February 2021 odoo 
Javascript :: setattribute disabled javascript 
Javascript :: js get string byte size 
Javascript :: button click javascript 
Javascript :: nodejs check directory exist or not 
Javascript :: how to check if window size of browser s changed javascript 
Javascript :: node google client api to get user profile with already fetched token 
Javascript :: codewars js Spinning Rings 
Javascript :: sin in javascript 
Javascript :: how to lock device orientation using css and javascript 
Javascript :: javascript format number 
Javascript :: Javascript noFill 
Javascript :: javascript dom last child 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =