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

Viewed 4403

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?

1 Answers

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.

Related