What is the use of withRouter and why components without withRouter are also used?

background:

some people say:

"withRouter can wrap any custom component, passing in three objects of react-router "s history,location,match.
there is no need to pass react-router attributes at one level. When you need to use the router
attribute, you can get the routing information you need by adding a layer of withRouter, to the component package.

others say:

if you use react-router-redux, you can get location directly from the router attribute in state
. You don"t need to use withRouter to get routing information anymore
Is

withRouter just to get this.props.location? are there any other examples that you haven"t noticed but withRouter has played a big role?

May.08,2021

The purpose of

is to enable the decorated components to obtain history , location , match ,
routing components directly from attributes, while non-routing components can only obtain these attributes through withRouter modification,
such as

.
<Route path='/' component={App}/>

App component can directly obtain these attributes in the route, but , if there is a sub-component Foo in the App component, then Foo cannot directly obtain the attributes in the route, but must be modified by withRouter .


Yes, just to get the parameters of location, which is essentially a high-level component that provides a context.

not all components that require routing parameters are directly connected to the route, and withRouter solves this problem.


personal understanding
for example:

 <Route exact path="/Home" component={Home}/>
 
 1.Route`this.props.location`
 2.`this.props.location``Route`
 `withRouter`

take a chestnut to say: when you need to use url to make judgments, or you need url information within the component, withRouter, the hoc-capable component, immediately has routing information, such as: query, params, location, url,search, etc.


originally there is such a good thing, so I have to pass routing attributes level by level every time the component is used. It's really annoying

.
Menu