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

this.props.history.location.push

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

this.props.history.location.push

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

PREVIOUS NEXT
Code Example
Javascript :: js to lowercase 
Javascript :: javascript if browser out of focus 
Javascript :: location of release apk in react native 
Javascript :: react pagination 
Javascript :: how to customize js alert box 
Javascript :: jquery when iframe is loaded 
Javascript :: round js 
Javascript :: unsplash 
Javascript :: nodejs console.log timestampt 
Javascript :: get all the child of the same class javascript 
Javascript :: uncaught typeerror is not a function javascript 
Javascript :: open pdf in browser javascript 
Javascript :: Vue.use is not a function 
Javascript :: nullish coalescing js 
Javascript :: for in loop javascript 
Javascript :: download file from api vue 
Javascript :: nodejs user input 
Javascript :: javascript arrow functions default parameter 
Javascript :: js check if div have another div 
Javascript :: jquery display modal bs4 
Javascript :: data type javascript 
Javascript :: add item to array in javascript 
Javascript :: how to know actual scroll js 
Javascript :: refresh page by hand in react 
Javascript :: expressjs async await 
Javascript :: simple kick command discord.js v12 
Javascript :: random id generator javascript 
Javascript :: run on load js 
Javascript :: moment compare time 
Javascript :: object json parse javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =