I have a simple app:
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Route } from 'react-router-dom';
import './css/hind.css';
import './css/splash.css';
import Feedback from './components/Feedback';
import NotFound from './components/NotFound';
render((
<BrowserRouter>
<div className="add-100 dark-background">
<Route path="/" exact={true} component={Feedback}/>
<Route path="*" component={NotFound}/>
</div>
</BrowserRouter>
), document.getElementById('app'));
And I would expect that at the url / I would see the first component, and at any other url, I would see the second. The NotFound part is displaying how I would expect it too, but at /, I see the first component, then the second component displayed beneath it. NotFound is definitely not in my Feedback file. How do I use the router correctly so I only display the component I want it to?
Wrap your routes with <Switch />.
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import './css/hind.css';
import './css/splash.css';
import Feedback from './components/Feedback';
import NotFound from './components/NotFound';
render((
<BrowserRouter>
<div className="add-100 dark-background">
<Switch>
<Route path="/" exact={true} component={Feedback}/>
<Route path="*" component={NotFound}/>
</Switch>
</div>
</BrowserRouter>
), document.getElementById('app'));
What does <Switch /> exactly do?
It renders the first child <Route> or <Redirect> that matches the location.
source
path="*" isn't actually supported in RRv4. Instead, a Route with no path property will always match. Combining the knowledge with the Switch component, you'll get the desired outcome. Switch will only render the first matching Route, so the idea is that if none of your other Routes match, then the last Route in your Switch component will render.
<Switch>
<Route path="/" exact={true} component={Feedback}/>
<Route component={NotFound}/>
</Switch>
Related
This question already has an answer here:
Why I receive blank page? React
(1 answer)
Closed last month.
I want to using the BrowserRouter component from the react-router-dom library to handle client-side routing in React.Js
In this case, when the user navigates to the every URL of the website, these pages component will be rendered. But nothing shows up to the root directory or any path.
Here is the code of app.js
import React from "react";
import GlobalStyle from "./globalStyle.js";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
//Pages
import Home from "./Pages/Home";
import Pricing from "./Pages/PricingPage.js";
import Signup from "./Pages/SignupPage.js";
function App() {
return (
<Router>
<GlobalStyle />
Test
<Routes>
<Route exact path="/" component={Home} />
<Route path="/signup" component={Signup} />
<Route path="/pricing" component={Pricing} />
</Routes>
</Router>
);
}
export default App;
If you are using react-router-dom v6, the routes should be:
<Route exact path="/" element={<Home />} />
<Route path="/signup" element={<Signup />} />
<Route path="/pricing" element={<Pricing />} />
component is element, their values are in tags.
I think that the problem is in the switch, in version of react-router-dom or in routes. Only when I created and applied the code in this class, the screen started don't render and stays white. I already changed the version of react-router-dom but I don't know what can be.
Below the code of routes.js:
//react-router-dom version: "^5.3.0"
import {BrowserRouter, Route, Switch} from 'react-router-dom';
import Home from './pages/Home';
import Header from './components/Header';
const Routes = () => {
return(
<BrowserRouter>
<Header/>
<Switch>
<Route exact path="/" component={Home}/>
</Switch>
</BrowserRouter>
)
}
export default Routes;
React Router v6 introduces some changes like.
Routes component that is kind of like Switch, but a lot more powerful one.
Route still exist but you don't pass a pointer to that component function or as a children component, Instead you pass the JSX element to the element prop &
exact doesn't exist anymore, now it's always looks for exact matches.
So, component={Home} would become element={<Home />}
Something like this
<Routes>
<Route path="/" element={<Home />} />
<Route path="about" element={<About />} />
</Routes>
You can dive deep to this migration guide: https://reactrouter.com/docs/en/v6/upgrading/v5
hope that's help you
Hoping someone can help me as I am at my wits end trying to figure out why this isn't loading. The site compiles with no errors but then just hangs loading and whitescreen; nothing in the DOM either to point out what is wrong.
Here is the source code (https://github.com/Asutherland8219/react-portfolio)
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />
, document.getElementById('root')
);
App.js
import React from 'react'
import './App.css';
import Navbar from './components/Navbar/Navbar';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'
import Home from './pages/Home';
import About from './pages/About';
import Portfolio from './pages/Portfolio';
import Contact from './pages/Contact';
function App() {
return (
<Router>
<Navbar />
<Switch>
<Route path='/' exact component= {Home}/>
<Route path='/about' component= {About}/>
<Route path='/portfolio' component= {Portfolio}/>
<Route path='/contact' component= {Contact}/>
</Switch>
</Router>
);
}
export default App;
There is an error in your Button component.
<Button
className={`btn ${checkButtonStyle} ${checkButtonSize}`}
onClick={onclick}
type={type}
>
{children}
</Button>
You are rendering Button in Button component.
Line 5 of your Navbar file says
import { Button } from '../Button/Button.css';
I find this weird, and this error may be the reason behind this error
function App() {
return (
<>
<Header/>
<Routes>
<Route path={HOMEPAGE_URL} element={<App/>}/>
..........
</Routes>
</>
);
}
In my case I used App as a component for the element, this will work like a recursive function and eventually kill the memory with a poup message.
I had this kind of error for the first time, where the website is loading all the time without any errors. Maybe this answer will help someone with the same problem.
I'm new to React Router so if this has been asked before maybe someone could point me in the right direction! Basically I have a WordPress install that I'm pulling in my websites data from through the API.
I've created custom routes to query my pages and my posts by slug.
Using react router I was able to create a template called Page.js which changes dynamically using the code below.
However, now I'm trying to do the same exact thing with the blog posts but the app isn't using Blog.js its still defaulting back to Page.js
here's my App.js code...
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import Home from './pages/Home';
import Page from './pages/Page';
import Blog from './pages/Blog';
import Header from './components/Header';
import Footer from './components/Footer';
class App extends React.Component {
render() {
return (
<Router>
<div>
<Header/>
<Route exact path="/" component={Home} />
<Route path="/:slug" component={Page} />
<Route path="/blog/:slug" component={Blog} />
<Footer/>
</div>
</Router>
);
}
}
export default App;
More Details:
Page.js works by checking const { slug } = this.props.match.params; and then querying WordPress using that slug to pull in the data it needs. In componentDidUpdate i'm checking prevProps to see if the slug matches the previous slug, if not it fetching the new data.
This works great and I was hoping to do the same in the Blog.js as well.
However, if this isn't the best approach please advise another method.
Two things:
Use element: This will allow only one route to be used, no composing. (See this documentation)
Check the order of path statements: Use defined paths before :param, this avoids considering /blog/:slug as a /:slug parameter.
`
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
class App extends React.Component {
render() {
return (
<Router>
<div>
<Header/>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/blog/:slug" component={Blog} />
<Route path="/:slug" component={Page} />
</Switch>
<Footer/>
</div>
</Router>
);
}
}
I think you're pretty close to the recommended implementation, just a few small tweaks should get you there.
First,
In your App.js file you're actually handling routing, without using the <Switch> component provided by React Router, replacing the <div> and </div> tags in your App.js file with <Switch> and </Switch> respectively should get this working for you. See below...
import React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; //make sure you import it also!
import Home from './pages/Home';
import Page from './pages/Page';
import Blog from './pages/Blog';
import Header from './components/Header';
import Footer from './components/Footer';
class App extends React.Component {
render() {
return (
<Router>
<Switch> //Add this in
<Header />
<Route exact path="/" component={Home} />
<Route path="/blog/:slug" component={Blog} />
<Route path="/:slug" component={Page} />
<Footer />
</Switch> //Add this in
</Router>
);
}
}
export default App;
I would recommend going further though!
To make these components more understandable, you should refactor routing functionality into a routes.js file, and top-level App component logic/structure into the App.js file. See below...
In App.js:
This file is where you should handle your base application structure and logic. For example this file is where you'll import your <Header>, your <Footer>, and where the Route component will render.
import * as React from 'react'
import Header from './../Header/Header.jsx'
import Footer from './../Footer/Footer.jsx'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
// Handle your top-level application state here
}
}
// define your top-level application functions here
render() {
return (
<div>
<Header />
<main>
{this.props.children} //This where the Route components will render
</main>
<Footer />
</div>
)
}
}
export default App
In Routes.js:
This file is where you should import your App component, and then handle the routing statements.
import React from 'react'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import App from './components/App'
import Home from './pages/Home'
import Page from './pages/Page'
import Blog from './pages/Blog'
/* construct routes */
export default () => {
return (
<Router>
<App>
<Switch>
<Route path='/' exact component={Home} />
<Route path='/blog/:slug' component={Blog} />
<Route path='/:slug' component={Page} />
</Switch>
</App>
</Router>
)
}
If you structure your application this way, your routing logic and top-level application logic are separate, and in the end your files will be less cluttered as both Route files and top-level App files can get fairly dense.
Hope this helps! Let me know if I can explain anything further.
I want to build my project with npm run build
and
when I build my project, all files are build correctly
and CSS file,favicon,manifest,js file are load.
But my js file does not show anything.
I am using BrowserRouter
I don't use webpack
This is my Route file:
import React , { Component } from 'react';
import OnePage from './OnePage/OnePage';
import Blog from './OnePage/Blog/Blog';
import Blogs from './OnePage/Blogs/Blogs';
import Home from './WebApp/Routes/Home';
import Register from './WebApp/Routes/Register/Register';
import Buy from './WebApp/Routes/Buy/Buy';
import Profile from './WebApp/Routes/Profile/Profile';
import Support from './WebApp/Routes/Support/Support';
import Search from './WebApp/Routes/Search/Search';
import SearchCategories from './WebApp/Routes/Search/SearchCategories';
import Users from './WebApp/Routes/Users/Users';
import Suggestion from './WebApp/Routes/Suggestion/Suggestion';
import './../public/appStyle.css';
import { Route, BrowserRouter as Router } from 'react-router-dom';
export default class App extends Component {
render(){
return(
<Router>
<div>
<Route exact path="/" component={OnePage}/>
<Route exact path="/app/" component={Home}/>
<Route exact path="/blog/:id" component={Blog}/>
<Route path="/blogs" component={Blogs}/>
<Route path="/app/register" component={Register}/>
<Route path="/app/buy" component={Buy}/>
<Route path="/app/profile/:id" component={Profile}/>
<Route path="/app/support" component={Support}/>
<Route path="/app/search" component={Search}/>
<Route path="/app/categories/:id" component={SearchCategories}/>
<Route path="/app/users" component={Users}/>
<Route path="/app/suggestion" component={Suggestion}/>
</div>
</Router>
);
}
}
What should I do?