score:2
Your switch is matching the route param first
<Route exact path="/items/:id" component={ItemPage} />
<Route exact path="/items/edit" component={EditPage} />
This is the same path i.e. edit
considered is a parameter. The below should work.
<Route exact path="/items/edit" component={EditPage} />
<Route exact path="/items/:id" component={ItemPage} />
Test with paths http://.../items/12
, http://.../items/edit
score:1
The problem is that /items/edit
assumes edit
as the :id
. You need to change the routes like this:
<Route exact path="/items/:id" component={ItemPage} />
<Route exact path="/items/:id/edit" component={EditPage} />
So, You can add other routes like this beautifully. I've provided a sandbox example for you. Click to view Sandbox
Screenshots:
P.S: if you insist not to provide id
for edit page, You need to change your routes completely. For example like this:
<Route exact path="/item/:id" component={ItemPage} />
<Route exact path="/items/edit" component={EditPage} />
score:1
You just type const two times on the item page. It is working fine on my hand. And you just have to change the position of the below routes
<Route exact path="/items/edit" component={EditPage} />
<Route exact path="/items/:id" component={ItemPage} />
// Relative imports
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
// Absolute imports
import YourReactComp from './YourReactComp.jsx';
const root = document.getElementById('root');
const MainPage= () => (
<div>Main Page</div>
);
const ItemPage= () => (
<div>Item Page</div>
);
const EditPage= () => (
<div>Edit Page</div>
);
const NoMatch = () => (
<p>No Match</p>
);
const RoutedApp = () => (
<BrowserRouter >
<Switch>
<Route exact path="/items/edit" component={EditPage} />
<Route exact path="/items/:id" component={ItemPage} />
<Route path="/yourReactComp" component={YourReactComp} />
<Route path="*" component={NoMatch} />
</Switch>
</BrowserRouter>
);
ReactDOM.render(<RoutedApp />, root);
Source: stackoverflow.com
Related Query
- React Router is not working fine with Exact and Switch
- 404 custom page not working with useContext and React Router
- React router is not working with webpack server and redux?
- React Router with custom history not working
- React router v4 not working with Redux
- React hook useRef not working with styled-components and typescript
- React Router Switch and exact path
- React router not working with params
- React router exact property is not working
- React Router 4 and exact path with dynamic param
- React - Ant Design, Form with getFieldDecorator and initialValue not working in single chechbox
- 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
- 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 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)
- 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
- Nested routes with react router v4 not working
- Protected Routes React Router 4 not working with auth state stored in Redux
- React Router / Redux / HOC not working with 'render' prop
- Django REST and React - JWT Cookie not getting set in browser but working with postman
- My 404 not found page with React router is not working
- React Router not working correctly on production with express backend
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
- React router NavLink activeClassName does not work with redux and styled components
More Query from same tag
- reactjs render span tag with ternary operator
- Play only one song at a time (React)
- How to add link to part of component and make clickable
- How can I get a word's bounding rectangle within a paragraph, while only knowing it's index?
- Uncaught TypeError: Cannot read property 'data' of null in React js
- Getting Table to Filter based on choices
- React.js modal not appearing as it should
- Pass 'this' object to component - Typescript / React
- React Checkbox Form State is Delayed
- Update apollo client with response of mutation
- Testing redirect after submit with React Testing Library
- how do I send a text field input from the frontend to the backend
- What is this React syntax – <ReactMarkdown components={{pre() {}}} />?
- My React Router doesn't work for all routes
- How to write a library similar to jquery in typescript
- setState getting updated multiple times
- Avoid rerendering child when passing object in props
- unable to load css in react js
- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type') redux reactjs
- How to share Ref using custom hooks?
- Styling Grid to have more elements than text
- convert props from javascript to typescript in react
- Styling material UI components
- How can I check only one check box at a time instead of all in a list of checkboxes in React
- admin on rest: display default value in create form
- Next.js passing additional data to dynamic routes
- error messages under the relevant input fields automatically from Django Rest API by react hooks
- onClick and onChange doesn't fire on radio buttons for safari in reactjs
- React-notification-system - trigger notification on JSON input
- React Leaflet: Failed to load sourcemap