Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of useRouteMatch for class-based components?

I want to use the useRouteMatch hook inside a class-based component in React because it simplifies passing parameter to component. Using this hook inside class component throws error. What is the equivalent of this hook to use in a React class?

like image 590
user1388835 Avatar asked Oct 21 '25 08:10

user1388835


1 Answers

You could use withRouter HOC, and then there will be match object in wrapped component props:

import React from 'react';
import {withRouter} from 'react-router-dom';

class SomeClassComponent extends React.Component {

  render(){
    console.log(this.props.match) // match object
    return(<span />)
  }
}

export default withRouter(SomeClassComponent)

like image 109
StackedQ Avatar answered Oct 23 '25 02:10

StackedQ