DEV Community

Dieldore
Dieldore

Posted on

can't declare url and path variables useRouteMatch in class

Now i do so:

constructor(props) {
    super(props);
    let r = useRouteMatch();
    this.path = r.path;
    this.url = r.url;
}

but this does not work and throws errors: url is not defined, if i use them in render()
This code is indicated in the documentation:

let {path, url} = useRouteMatch();

It works for functional programming, not for class. How to declare these variables in this as one line? Or something

Top comments (1)

Collapse
 
sergiodxa profile image
Sergio Daniel Xalambrí

React Hook can’t be used in class based components, but what you can do is to create a function component where you call the hook and pass the returned value to your class based component as a prop, create a HOC or Render Prop from the hook for example, then you will be able to receive those values in your class based component.