Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of exact and strict props

Tags:

I am working with React-Router in React-JS:

The <Route> is an built in component and have two different props: exact and strict

Problem

The documentation does not clearly defines the differences in between the exact and strict .

Kindly help me. The document is very confusing and not clear at that point.

like image 970
Ishmal Ijaz Avatar asked Sep 11 '18 11:09

Ishmal Ijaz


People also ask

What is the use of exact in react router?

The exact param disables the partial matching for a route and makes sure that it only returns the route if the path is an EXACT match to the current url.

Why we need exact props in react router dom?

The exact prop is used to define if there is an exactly the requested path. Usually it is used to wrap routes without child-routes (e.g. homepage). First route will catch only routes like mydomain.com , mydomain.com/ , mydomain.com/?

What is the use of the prop in ReactJS?

In ReactJS, the props are a type of object where the value of attributes of a tag is stored. The word “props” implies “properties”, and its working functionality is quite similar to HTML attributes. Basically, these props components are read-only components.

Which component is used to render a Route?

The Route component is perhaps the most important component in React Router to understand and learn to use well. Its most basic responsibility is to render some UI when its path matches the current URL. The “react-empty” comments are just implementation details of React's null rendering.


2 Answers

Use case 1

If you use exact and strict together, then the location.pathname will only match exactly as provided in path props.

Example:

<Route exact strict path="/one/" component={About}/> 

Will only match /one/ but not /one and /one/two.

Use case 2

If you use only strict, then the location.pathname will match which have trailing slash.

Example:

<Route strict path="/one/" component={About}/> 

Will match /one/ and /one/two but not /one.

Use case 3

If you use only exact, then the location.pathname will match exact location path.

Example:

<Route exact path="/one" component={About}/> 

Will match /one or /one/. The exact props doesn't care for trailing slash. But it will not match /one/two.

like image 164
Bhojendra Rauniyar Avatar answered Sep 21 '22 13:09

Bhojendra Rauniyar


ReactRouter's strict prop defines if there is a strict entry of requested path in pathname, as described in docs. For example, if you wish not to handle the page's route without trailing slash, your Route can be described like this:

<Route path="/mypath/" strict ... /> 

So the pathname /mypath won't be handled with this Route, and the pathname /mypath/ will be. Note, that in this mode this Route will also catch other child-routes, e.g. /mypath/childroute, /mypath/childroute/childroute2, etc, but it won't catch route /mypath?query=.... Think about this prop like if you are using "string".includes("substring"):

"/mypath".includes("/mypath/")       => false "/mypath/".includes("/mypath/")      => true "/mypath/kappa".includes("/mypath/") => true 

The exact prop is used to define if there is an exactly the requested path. Usually it is used to wrap routes without child-routes (e.g. homepage).

<Route path="/" exact ... /> <Route path="/" ... /> 

First route will catch only routes like mydomain.com, mydomain.com/, mydomain.com/?query=... etc. The second will catch all routes, e.g. both of mydomain.com and mydomain.com/myroute.

like image 21
Limbo Avatar answered Sep 18 '22 13:09

Limbo