How To Use this.props.history.push On Your React Project

In this article, you will learn how to use this.props.history.push in your react project. The history.push() function belongs to react-router-dom and used to move from…

The post How To Use this.props.history.push On Your React Project appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa

In this article, you will learn how to use this.props.history.push in your react project.

The history.push() function belongs to react-router-dom and used to move from the current page to another one. It takes the first argument as a destination path and a second argument as the state.

history.push(path, [state])

Let’s say we want to move to the dashboard page, all we need to do is:

class MyComponent extends React.Component { 
  ... 
  myFunction() { 
    this.props.history.push("/dashboard"); 
  } 
  ... 
} 

export default withRouter(MyComponent);

We can also bring the data from the current page to our destination page using the second argument of this function:

class MyComponent extends React.Component { 
  ... 
  myFunction() { 
    this.props.history.push("/dashboard", { state: 'sample data'}); 
  } 
  ... 
} 

export default withRouter(MyComponent);

Note: You can only use this.props.history.push() function inside the component page that already listed on your project route, or simply use the withRouter() HOC if the component is not available in the route list.

The post How To Use this.props.history.push On Your React Project appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Pandu Rijal Pasa


Print Share Comment Cite Upload Translate Updates
APA

Pandu Rijal Pasa | Sciencx (2021-02-21T11:19:05+00:00) How To Use this.props.history.push On Your React Project. Retrieved from https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/

MLA
" » How To Use this.props.history.push On Your React Project." Pandu Rijal Pasa | Sciencx - Sunday February 21, 2021, https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/
HARVARD
Pandu Rijal Pasa | Sciencx Sunday February 21, 2021 » How To Use this.props.history.push On Your React Project., viewed ,<https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/>
VANCOUVER
Pandu Rijal Pasa | Sciencx - » How To Use this.props.history.push On Your React Project. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/
CHICAGO
" » How To Use this.props.history.push On Your React Project." Pandu Rijal Pasa | Sciencx - Accessed . https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/
IEEE
" » How To Use this.props.history.push On Your React Project." Pandu Rijal Pasa | Sciencx [Online]. Available: https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/. [Accessed: ]
rf:citation
» How To Use this.props.history.push On Your React Project | Pandu Rijal Pasa | Sciencx | https://www.scien.cx/2021/02/21/how-to-use-this-props-history-push-on-your-react-project/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.