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?
Asked
Active
Viewed 453 times
-3
-
https://reacttraining.com/react-router/web/api/withRouter – jonrsharpe Sep 09 '19 at 07:14
2 Answers
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