julz oh
julz oh

Reputation: 325

How to use useHistory() hook in Class Component Type Script

Hello I am trying to use

this.history.push({
                    pathname: `/search-results`,
                    search: `${job}$${location}`
                  }) 

In my class component that is using type script . However it is giving me back an error of Property 'history' does not exist on type 'Banner'. TS2339

I can see other examples of functional component, you can do const history = useHistory(); and use history.push from hook.

How can I do the same thing in class component?

Upvotes: 7

Views: 5265

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187024

Hooks are not designed to work with class based components.

However, react-router has other methods to support class based components such as the withRouter higher order component.

That said, use functional components and hooks if you can. They are the React standard going forward.

Upvotes: 5

Related Questions