Skip to content

can't declare url and path variables useRouteMatch in class

dieldore profile image Dieldore github logo ・1 min read

Now i do so:

constructor(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

twitter logo DISCUSS (1)
markdown guide

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.

Classic DEV Post from Aug 4 '19

You're not worth hiring unless...

Dieldore profile image