score:465
In this example, nothing really. The exact
param comes into play when you have multiple paths that have similar names:
For example, imagine we had a Users
component that displayed a list of users. We also have a CreateUser
component that is used to create users. The url for CreateUsers
should be nested under Users
. So our setup could look something like this:
<Switch>
<Route path="/users" component={Users} />
<Route path="/users/create" component={CreateUser} />
</Switch>
Now the problem here, when we go to http://app.com/users
the router will go through all of our defined routes and return the FIRST match it finds. So in this case, it would find the Users
route first and then return it. All good.
But, if we went to http://app.com/users/create
, it would again go through all of our defined routes and return the FIRST match it finds. React router does partial matching, so /users
partially matches /users/create
, so it would incorrectly return the Users
route again!
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.
So in this case, we should add exact
to our Users
route so that it will only match on /users
:
<Switch>
<Route exact path="/users" component={Users} />
<Route path="/users/create" component={CreateUser} />
</Switch>
score:-6
The shortest answer is
Please try this.
<switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/shop" component={Shop} />
</switch>
score:-5
Please try this.
<Router>
<div>
<Route exact path="/" component={Home} />
<Route path="/news" component={NewsFeed} />
</div>
</Router>
score:-2
By using exact, you can make sure that the contents of the homepage component will not appear on the other pages.
This is the scenario without using exact:
HOMEPAGE
Location: /
-----------------
homepage content
-----------------
SECOND PAGE
Location: /second-page
-----------------
homepage content
-----------------
-----------------
second content
-----------------
==========================================
Using exact:
HOMEPAGE
Location: /
-----------------
homepage content
-----------------
SECOND PAGE
Location: /second-page
-----------------
second content
-----------------
score:13
Take a look here: https://reacttraining.com/react-router/core/api/Route/exact-bool
exact: bool
When true, will only match if the path matches the location.pathname
exactly.
**path** **location.pathname** **exact** **matches?**
/one /one/two true no
/one /one/two false yes
score:28
In short, if you have multiple routes defined for your app's routing, enclosed with Switch
component like this;
<Switch>
<Route exact path="/" component={Home} />
<Route path="/detail" component={Detail} />
<Route exact path="/functions" component={Functions} />
<Route path="/functions/:functionName" component={FunctionDetails} />
</Switch>
Then you have to put exact
keyword to the Route which it's path is also included by another Route's path. For example home path /
is included in all paths so it needs to have exact
keyword to differentiate it from other paths which start with /
. The reason is also similar to /functions
path. If you want to use another route path like /functions-detail
or /functions/open-door
which includes /functions
in it then you need to use exact
for the /functions
route.
Source: stackoverflow.com
Related Query
- React : difference between <Route exact path="/" /> and <Route path="/" />
- What is the difference between useHistory() and props.history in React Route
- React route with exact path and param troubles
- What is the difference between React Native and React?
- What's the difference between "super()" and "super(props)" in React when using es6 classes?
- Difference between component and container in react redux
- Difference between React Component and React Element
- What's the difference between hydrate() and render() in React 16?
- What is the difference between NextJs and Create React App
- react router difference between component and render
- Difference between import React and import { Component } syntax
- What is the difference between hashHistory and browserHistory in react router?
- React Navigation V2: Difference between navigation.push and navigation.navigate
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- difference between React router.push and router.replace?
- What is the main difference between React Query and Redux?
- What's the difference between a JavaScript function and a React hook?
- React - What is the difference between renderToString and renderToStaticMarkup
- Difference between with and without useEffect in react functional component
- What's the difference between importing React's Fragment from React and React, { Fragment }?
- React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?
- React Router Switch and exact path
- What is the difference between React component instance property and state property?
- What is the difference between React component and React component instance?
- React Native - Difference between onChangeText and onSubmitEditing?
- How does React router works and what is the difference between <link> and<Route>
- What is the difference between arrow functions and regular functions inside React functional components (no longer using class components)?
- Difference between React Context and JS singleton
- ES6 React - What are the difference between reference, shallow copy and deep copy and how to compare them?
- what is the difference between getDefaultProps and getInitialState react js
More Query from same tag
- Why do I see the warning about using act when testing this hook?
- YUP validation: compare mathematical values of fields 'a' and 'b' to field 'c'
- What is wrong in the below code of input validation?
- How to use JSX as content for InfoWindow from Google Maps API?
- Listen for keypress / keydown event locally in react router component
- I cannot import fontawesome icons in my react project. (reactv6)
- data is not appending in new FormData in react
- How to redirect user back to initially-requested page after authentication with React-Router v6?
- Regex replace for preceding zero twice in string with different condition
- React-Redux: Component didn't re-render even props are changed
- How can i give link to download(not open) pdf file in reactjs
- npm run build Reactjs errno1
- React-bootstrap-table2 doesn't show data from Firebase
- Reactjs Onchange input for function
- Remove Bottom Borders from DataGrid in Material UI (mui)
- Django foreign keys queryset displayed in react-redux
- Why does not it work- inputProps:{{maxLength:12}} - Mui
- The value of textbox does not update in React
- How to change color button onclick react.js
- Add state and lifecycle methods to Function Components
- React- TypeError - this.state.movies.map
- TypeError: Cannot read property 'view' of undefined [FullCalendar-React] *solved
- How do I use a dynamic variable in a react function
- Attempted import error: 'PostsReducer' is not exported from './store/reducers/posts_reducer'
- Getting async data in a react material table component
- React i18next and correct way of changing language
- React useState with sorted array does not cause re-render of SemanticUI table if initial sorting is done
- how to prevent react-router-dom nested routes to get confused with route params
- Secure payment with paypal
- Axios : call function after response