score:2
The explanation is that the Switch
component looks only at its direct children for the path
property regardless of whether or not they are Route
s.
In this case, since the direct child of Switch
is User.Provider
with no path
property the switch will always just render that child (a switch will always render at least one child).
Now the children of User.Provider
will be rendered, and the Switch
will no longer have any bearing. This means now that the <Route path="*" />
will ALWAYS render like you are seeing.
Solution:
The solution is to just move the location of the User.Provider
to surround the Switch
component.
const Navbar = React.memo(props => {
const [user, setUser] = React.useState(null);
const value = React.useMemo(() => ({user, setUser}), [user, setUser]);
const history = useHistory();
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/old-match">Old Match, to be redirected</Link>
</li>
<li>
<Link to="/will-match">Will Match</Link>
</li>
<li>
<Link to="/will-not-match">Will Not Match</Link>
</li>
<li>
<Link to="/also/will/not/match">Also Will Not Match</Link>
</li>
</ul>
<User.Provider value = {value}>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/old-match">
<Redirect to="/will-match" />
</Route>
<Route path="/will-match">
<WillMatch />
</Route>
<Route path="*">
<NoMatch />
</Route>
</Switch>
</User.Provider>
</div>
</Router>
);
})
Source: stackoverflow.com
Related Query
- 404 custom page not working with useContext and React Router
- My 404 not found page with React router is not working
- React Router with custom history not working
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
- react router and routes is not working , i could not able to navigate to blogs page
- React router is not working with webpack server and redux?
- React 404 page not working with CSSTransition
- React Router is not working fine with Exact and Switch
- React JS Switch Router with admin only pages not hitting my 404 page
- React router v4 not working with Redux
- React hook useRef not working with styled-components and typescript
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- React router not working with params
- React Router - Go back to the last page visited, and not the default component
- React - Ant Design, Form with getFieldDecorator and initialValue not working in single chechbox
- React Router with a custom root and a base component
- Sending state with react router not working
- webpack and react jsx - hot loading not working with docker container
- Material UI AppBar with React Router 4 Implementation not working
- React Router - 404 page always showing due to custom route component
- How to fix "TypeError: items.map is not a function" when working with React and APIs?
- Reactjs react router dom useParams not working with dynamic url
- Bootstrap dropdown not working with React router
- Routes are not working with React router v4
- React - Material UI - TextField controlled input with custom input component not working properly losing focus
- React router v4 broswer history not working with code splitting
- Why is my ReactiveUserControl or React application not working with Selenium auto test and WinAppDriver (C# test solution)
- React Router V4 - Route between components and not entire page
- WebSocket SocketIO connection not working with NestJS server on Heroku and react client on Vercel
- Redirect with state not working in React Router 4.x
More Query from same tag
- React container reusability (with Redux, Immutable and Reselect)
- TypeError: Class extends value #<Object> is not a constructor or null in react js
- Why the original data is mutated in this simple task?
- Set in immutable Map without override the previous value
- POST FormData to JSON object array with ReacStrap Form and Express
- React.js - Functional component with useState hook doesn't re-render as expected
- Can I put a conditional here in the value?
- Payload undefined when try to delete selected id?
- Browser autocomplete issue
- Cannot read property 'question' of undefined
- Uncaught (in promise) ReferenceError: <function> is not defined
- React, Why label not firing onChange for checkbox?
- Can't increasing the value in front end by react. Data comes from mongodb
- inline styles for tabs in material ui doesn't change color
- Displaying Images in React
- webpack Invalid Host header when accessing from domain, not when through IP
- How to use @Action in Mobx + reactjs?
- useContext working not working with nested component
- Unable to convert class component to function component
- Error in posting JSON form data to REST api using AJAX in React
- React Redux - Reducer with CRUD - best practices?
- How to use useState to only update specific object from array of objects?
- React change file without rebuild and restart
- How to call react-modal from rails-assets.org in react-rails component
- react table row show column on button click
- How to use state inside a function called by onSubmit?
- my function for filtering isnt working properly it dosent register first change in input field
- ScrollIntoView() can find the html element
- Fetching the data in React
- React Typescript: How run some lines at the same time as each other, and other parts need to wait