0

For the functional component, I have used withRouter for redirect to another component like below:

    const buttonClick = (history) => {
      history.push('/spot');
    };
   const Topleft = withRouter(({history}) => {
     return (  );
   });

   export default Topleft;

I want to does exactly the same thing for the class component. Like Below:

class TopLeft component {
 render(){
   return()
  }
}

 export default Topleft;

How can I do that? Can anyone please help.

4
  • you can get history or location object from class component props. Just use HOC and wrap Topleft: export default withRouter(Topleft); Commented Jan 29, 2020 at 9:50
  • export default withRouter(Topleft) will work Commented Jan 29, 2020 at 9:51
  • @demkovych How to pass history in that case? Commented Jan 29, 2020 at 9:51
  • @MehadiHassan this.props.history will work then Commented Jan 29, 2020 at 9:52

1 Answer 1

1

You can get access to the history object's properties and the closest 's match via the withRouter higher-order component.

const { match, location, history } = this.props;
...
export default withRouter(Topleft);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.