Since 3 days i am working on react route but its not working..I
I dont know where i am doing wrong even simple route is not working...if simple routing(without params) work then i wanted to pass ID to the NEW component..
But i am able to pass ID optionally ..IF i remove ? then routing doesnt works.. I have tried using switch and exact too..
And One More issue is by passing ID optionally i am getting Undefined in New Component Page During render (First time)..So i wanted to pass ID:1 from router .Please help me out...Thanks
<Route name="ideas" path="/Hello" component={New} /> (Not Working)
<Route name="ideas" path="/:Hello" component={New} /> (Not working)
<Route name="ideas" path="/New/:Hello?" component={New} />(Not working)
<Route name="ideas" path="/" component={New} /> {Working}
<Route name="ideas" path="/:Hello?" component={New} /> (working)
import React from 'react';
import ReactDOM from'react-dom'
import New from './New'
import Demo from "./demo";
import {
BrowserRouter as Router,
Route,
Switch,
} from 'react-router-dom'
function App() {
return (
<div>
<Router>
<Route name="ideas" path="/Hello" component={New} />
</Router>
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById("root")
);
export default App;
I updated your codesandbox. You were not using Switch from react-router-dom. Which is necessary when dealing wit routes. Please check updated codesandbox.
Use it in following way
import React from "react";
import ReactDOM from "react-dom";
import New from "./New";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function App() {
return (
<div>
<Router>
<Switch>
<Route name="ideas" path="/Hello" component={New} />
</Switch>
</Router>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
You can also have a look at https://codesandbox.io/s/smoosh-currying-8job8.
Updated version of your code:
https://codesandbox.io/s/unruffled-hellman-2f19h
Related
I'm working on my portfolio. I'm trying to put all components (Home, Skills, Projects, Contact) on one page, so I'm not planning to set routes for those component. However I want to set a route for project detail component, which I'll put a link on Projects component. I also want to set a route for 404 page.
I wrote the following code but it doesn't know ProjectDetail page when I access http://localhost:3000/projects. Can you give me advice how I can fix the problem?
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import Skills from './Skills';
import Projects from './Projects';
import ProjectDetail from './ProjectDetail';
import Contact from './Contact';
import Error from './Error';
const App = () => {
return (
<>
<Router>
<Home />
<Skills />
<Projects />
<Contact />
<Routes>
<Route path='/projects' element={<ProjectDetail />} />
<Route path='*' element={<Error />} />
</Routes>
</Router>
</>
);
};
export default App;
I think you need to wrap all the components that u want in one page and put it on a route like this: <Route path="/" element={<OnePageApp/>}/>
After this i don't see any other errors.
Good Luck, I hope i have been able to help you
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import Skills from './Skills';
import Projects from './Projects';
import ProjectDetail from './ProjectDetail';
import Contact from './Contact';
import Error from './Error';
const App = () => {
return (
<>
<Router>
<Home />
<Skills />
<Projects />
<Contact />
<Routes>
<Route path='/projects' element={ProjectDetail} />
<Route path='*' element={Error} />
</Routes>
</Router>
</>
);
};
export default App;
I am trying to implement a dynamic route from my app.js.
I have 2 pages. HomePage.js and ReportPageLayout.js.
When I try to navigate from HomePage to ReportPage, I am getting a status= cancelled.
Image of Network tab
When i refresh the page in the browser, the data is fetched and rendered.Image status=200 and component rendered on dom
Here is my app.js file
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Homepage from './pages/Homepage';
import ReportPage from './pages/ReportPageLayout';
export default function App() {
return (
<div className="App">
<Router>
<Switch>
<Route exact path="/" component={Homepage} />
<Route path="/:id" component={ReportPage} />
</Switch>
</Router>
</div>
);
}
Please help. Thank you in advance.
As far as I know (not that much), in router-dom v5 you should nest the target component and not as an argument
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Homepage from './pages/Homepage';
import ReportPage from './pages/ReportPageLayout';
export default function App() {
return (
<Fragment>
<Switch>
<Route exact path="/">
<Homepage/>
</Route>
<Route path="/:id">
<ReportPage/>
</Route>
</Switch>
</Fragment>
);
}
Furthermore, if you use suspense or importing data from, you should always create a fallback.
I had the same thing here
Delay in loading the array crushs the app
I am trying to render an ultra-simple webpage using react-routing-dom lib. Here is my app.js
import React from 'react';
import Nav from './nav.js';
import Shop from './shop.js';
import About from './about.js';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import './App.css';
function App() {
return (
<>
<Nav />
<Router>
<Route exact path='/' element={<About />} />
<Route exact path='/shop' element={<Shop />} />
</Router>
</>
);
}
export default App;
using this, I run npm start and expect that the <Nav /> and <About /> components render at http://localhost:3000. Instead, the screen is completely blank. There are no warnings, errors, etc.; it's a clean build. Not even the <Nav /> component that is outside the entire Router block is rendered!
The plot thickens: when I build and run the following, all three components render.
import React from 'react';
import Nav from './nav.js';
import Shop from './shop.js';
import About from './about.js';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import './App.css';
function App() {
return (
<>
<Nav />
<Router>
<About />
<Shop />
</Router>
</>
);
}
export default App;
From this, I think its safe to assume that <Route /> is the problem.
react-router-dom seems to change a lot in a relatively short amount of time, so there is a lot of outdated info. If someone could point me in the right direction, I would be very grateful indeed.
Only thing I see missing is the Routes component wrapping the Route components. You imported Routes but don't appear to have used it. All Route components must be rendered into a Routes component (or another Route if nesting). Without the Routes you should be seeing the error A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>.
<Router>
<Routes>
<Route exact path="/" element={<About />} />
<Route exact path="/shop" element={<Shop />} />
</Routes>
</Router>
Check if you have installed react-router-dom version 6. for version 6 you must wrap up all 'Route' inside a "Routes".
React router can't match any route and always shows last (NotFound) component. Everything works fine if I use BrowserRouter from 'react-router-dom' instead of Router. But I have to use Router because I have to use custom history (for which I am using history library). Heres my code.
import { Router, Route, Switch } from 'react-router-dom';
import { createBrowserHistory } from 'history';
import ExpenseDashboardPage from '../components/ExpenseDashboardPage';
import AddExpensePage from '../components/AddExpensePage';
import EditExpensePage from '../components/EditExpensePage';
import HelpPage from '../components/HelpPage';
import NotFound from '../components/NotFound';
import Header from '../components/Header';
import LoginPage from '../components/LoginPage';
export let history = createBrowserHistory();
function AppRouter() {
return (
<div>
<Router history={history}>
<Header />
<Switch>
<Route path="/" component={LoginPage} exact={true} />
<Route path="/dashboard" component={ExpenseDashboardPage} />
<Route path="/create" component={AddExpensePage} />
<Route path="/edit/:id" component={EditExpensePage} />
<Route path="/help" component={HelpPage} />
<Route component={NotFound} />
</Switch>
</Router>
</div>
);
}
export default AppRouter;
It looks like react-router-dom version 5.x is not compatible with history version 5.0.0. I switched to history version 4.x and now its working as expected.
I know this doesn't fit the question but in my case, i included a different component in Switch component. Switch component should only have Route components in them.
I used BrowserRouter with his basename, my server is WINSCP, the routes works correctly but, when I refresh it or writing it manually, I get :
My App.js is :
import React, { Component } from 'react';
import { Route, Switch, BrowserRouter} from "react-router-dom";
import { BackTop } from 'antd';
import Header from './components/Header/Header';
import Agenda from './components/Agenda/Agenda';
import Planning from './components/Planning/Planning';
import CreerActivite from './components/CreerActivite/CreerActivite';
import TypesRDV from './components/TypesRDV/TypesRDV';
class App extends Component {
render() {
return (
<div>
<BrowserRouter basename="/ReactCalendar">
<Header/>
<Switch>
<Route exact path="/" component={Planning} />
<Route exact path="/creerActivite" component={CreerActivite} />
<Route exact path="/typesRDV" component={TypesRDV} />
</Switch>
</BrowserRouter>
<BackTop />
</div>
);
}
}
export default App;
On my package.json, I have "homepage": "https://dev/ReactCalendar" and my folder on WINSCP is /dev/ReactCalendar/
How can I fix it ?
The reason why this is happening is your server does not know what to serve when you hit that URL. There are multiple approaches to solving your problem. I'll suggest the easiest approach here.
Replace BrowserRouter with HashRouter.
class App extends Component {
render() {
return (
<div>
<HashRouter basename="/ReactCalendar">
<Header/>
<Switch>
<Route exact path="/" component={Planning} />
<Route exact path="/creerActivite" component={CreerActivite} />
<Route exact path="/typesRDV" component={TypesRDV} />
</Switch>
</HashRouter>
<BackTop />
</div>
);
}
}
And obviously, don't forget to import HashRouter from 'react-router-dom'.
You can view other approaches here:
React-router urls don't work when refreshing or writing manually