How to redirect a page using ReactJS?

There are many ways we can redirect pages using ReactJS. We have used the history object. JavaScript history object is useful in this case. We need the make sure the component that wants to do this redirect need to have the history object available.

If we have the history object available to the props then we can use props inside the class-based component or we can use context like below:

componentFunc() {
   // Using props 
   this.props.history.push('/path');

   // Using context
   this.context.history.push('/path');
}
Share