-3

I am going the tutorial of react-router, where I encountered the topic of withRouter HOC. Why it is provided in the react-router library and how to use it?

kaushik_pm
  • 295
  • 3
  • 10

2 Answers2

1

It is needed whenever you want to have access to the match, history or location object of react-router. For basic functionality you can get away with Switch , Route and Link, but if you want to implement similar components yourself, or just want to access router functions programmatically in javascript code, you will have to use withRouter or useRouter respectively.

Nappy
  • 3,016
  • 27
  • 39
0

withRouter is a higher-order component provided by react-router-dom which gives you access to history, match and location object from props of particular object which is wrapped with withRouter.

Devang Padhiyar
  • 3,427
  • 2
  • 22
  • 42