This question already has an answer here:
Why I receive blank page? React
(1 answer)
Closed 19 hours ago.
I'm trying my hand at studying react. I want to create a simple template from bootstrap components, but react doesn't see the components when compiling.
app.js
import React from "react";
import { BrowserRouter as Router, Route, Routes} from 'react-router-dom';
import Layout from './hocs/Layout';
import Home from './components/Home';
import Blog from './components/Blog';
import BlogDetail from './components/BlogDetail';
import Category from './components/Category';
function App() {
return(
<Router>
<Layout>
<Routes>
<Route exact path='/' component={Home} />
<Route exact path='/blog' component={Blog} />
<Route exact path='/blog/:id' component={BlogDetail} />
<Route exact path='/category/:id' component={Category} />
</Routes>
</Layout>
</Router>
);
};
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Home.js
import React from "react";
function Home(){
return(
<div class="card mb-3">
<div class="row g-0">
<div class="col-md-4">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
<p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
</div>
</div>
</div>
</div>
);
};
export default Home;
import Navbar from '../components/Navbar'
const Layout = (props) =>(
<div>
<Navbar />
{props.children}
</div>
);
export default Layout
p.s. Navbar react sees, but the home component does not. There is no code even in the console, although they are identical in content.
Each route should show the layout navbar and load the component that is called by the route. Layout and Navbar are seen by the reactor, but everything else is not even reflected in the browser console.
You can upgrade to v6 and use an Outlet component in your Layout to do what you want using react-router-dom.
The Outlet allows nested UI to show up when child routes are rendered.
import { Outlet } from 'react-router-dom';
import Navbar from '../components/Navbar'
const Layout = () => {
return (
<div>
<Navbar />
<main>
<Outlet />
</main>
</div>
);
};
export default Layout;
Also, your App component would need to be refactored like this:
function App() {
return(
<Router>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path='blog' element={<Blog />} />
<Route path='blog/:id' element={<BlogDetail />} />
<Route path='category/:id' element={<Category />} />
</Route>
</Routes>
</Router>
);
};
Related
I have downloaded everything and it is not showing or displaying anything on the page. Here is the error that is showing in the inspect
Here is the code
This is App.js
//import logo from './logo.svg';
import './App.css';
import { BrowserRouter , Routes, Route } from 'react-router-dom';
import HomeScreen from './screens/HomeScreen';
function App() {
return (
<BrowserRouter>
<div>
<header>
VAC
</header>
<main>
<Routes>
<Route path="/" element={<HomeScreen/>}/>
</Routes>
</main>
</div>
</BrowserRouter>
);
}
export default App;
This is HomeScreen.js
import data from '../data';
function HomeScreen() {
return (
<div>
<h1>Features Products </h1>
<div className="products">
{data.products.map((product) => (
<div className="product" key={product.slug}>
<a href={`/product/${product.slug}`}>
<img src={product.image} alt={product.name} />
</a>
<div className="product-info">
<a href={`/product/${product.slug}`}>
<p>{product.name}</p>
</a>
<p>
<strong>${product.price}</strong>
</p>
<button> Add To Cart</button>
</div>
</div>
))}
</div>
</div>
);
}
export default HomeScreen;
where can be the mistake? please help me
Let's try to break your App a bit, so it will be clearer what happens.
Layout that is same for all routes should be provided in toplevel route, not around Routes. Outlet inside Layout component will be replaced with particular route.
For more about Outlet check official documentation, I warmly recommend this tutorial.
import './App.css';
import { BrowserRouter , Routes, Route } from 'react-router-dom';
import HomeScreen from './screens/HomeScreen';
function Layout() {
return (
<div>
<header>
VAC
</header>
<main>
<Outlet />
</main>
</div>
);
}
function App() {
return (
<Routes>
{/* Here we provide styles through Layout component. */}
<Route path="/" element={<Layout />}>
<Route index element={<HomeScreen />} />
</Route>
</Routes>
);
}
/* It's good idea to separate providers from App. */
function WrappedApp() {
return (
<BrowserRouter>
<App />
</BrowserRouter>
);
}
export default WrappedApp;
If still nothing is displayed, please right click on webpage and in menu choose "inspect page source" and please provide what it shows. Additionaly it would also be useful to see index.ts file.
import { BrowserRouter as Router } from "react-router-dom";
import { render } from "react-dom";
import App from "./components/App";
render(
<Router>
<App />
</Router>,
document.getElementById("app")
);
// App.js
import React from "react";
import { Route, Switch } from "react-router-dom";
import HomePage from "./home/HomePage";
import AboutPage from "./about/AboutPage";
import Header from "./common/Header";
import PageNotFound from "./PageNotFound";
function App() {
return (
<div className="container-fluid">
<Header />
<Switch>
<Route exact path="/" component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route component={PageNotFound} />
</Switch>
</div>
);
}
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
This question already has answers here:
Error: useHref() may be used only in the context of a <Router> component. It works when I directly put the url as localhost:3000/experiences
(11 answers)
Closed 3 months ago.
I try to make site with router, but using anchor tag <a> reloads the page. When I changed them to NavLink component my site broke. I see white page and many errors in devtools.
App.js
import "./App.css";
import "./Components/MainContent/Main/MainContent.module.css"
import "./Components/Header/Header.module.css"
import "./Components/Navbar/Navbar.module.css"
import Header from "./Components/Header/Header";
import Navbar from "./Components/Navbar/Navbar";
import MainContent from "./Components/MainContent/Main/MainContent";
import { Router, Route, Routes, BrowserRouter } from "react-router-dom";
import Catalog from "./Components/MainContent/Catalog/Catalog"
import Busket from "./Components/MainContent/Busket/Busket"
import Contacts from "./Components/MainContent/Contacts/Contacts"
function App() {
return (
<div className="appWeb">
<Header />
<Navbar />
<BrowserRouter>
<Routes>
<Route path='/' element={<MainContent />} />
<Route path='/index.html' element={<MainContent />} />
<Route path='/Catalog' element={<Catalog />} />
<Route path='/Busket' element={<Busket />} />
<Route path='/Contacts' element={<Contacts />} />
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
Navbar.js
import React from "react";
import styles from "./Navbar.module.css"
import { NavLink } from "react-router-dom";
function Navbar() {
return (
<nav className={styles.navbar}>
<div className={styles.navbar__links}>
<NavLink to="/">Главная</NavLink>
</div>
<div className={styles.navbar__links}>
<NavLink to="Catalog">Каталог</NavLink>
</div>
<div className={styles.navbar__links}>
<NavLink to="/Busket">Корзина</NavLink>
</div>
<div className={styles.navbar__links}>
<NavLink to="/Contacts">Контакты</NavLink>
</div>
</nav>
);
}
export default Navbar;
To be working, NavLinks should be nested inside a Router component. So your Navbar containing the links should move from outside the router:
<Navbar/>
<BrowserRouter>
<Routes>
...
To inside the router:
<BrowserRouter>
<Navbar/>
<Routes>
...
I'm creating a Google Clone. This is my current code for that. I read that I needed to changed the syntax from 'Switch' to 'Routes', given the update for react-router. I did just that and my "This is the search page" is not displaying inside of the browser.
import React from "react";
import './App.css';
import Home from './pages/Home';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
function App() {
return (
// BEM
<div className="app">
<Router>
<Routes>
<Route path="/search">
<h1>This is the search page</h1>
</Route>
<Route path="/">
<Home />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
you need to create each component speratly some things like this:
import React from 'react;
function Home(){
return(
<div>
<h1>Home</h1>
</div>
);
}
export default Home;
import React from 'react;
function Search(){
return(
<div>
<h1>Search</h1>
</div>
);
}
export default Search;
change index.js to :
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById("root"));
change App.js to
import React, {useEffect} from 'react';
import ReactDOM from "react-dom";
import { BrowserRouter ,Routes,Route} from 'react-router-dom';
import Search from '.........'
import Home from '.......'
function App(){
return(
<BrowserRouter>
<Routes>
<Route exact path="/" element={<Home/>} />
<Route path="/search" element={<Search/>} />
<Routes>
</BrowserRouter>
);
}
export default App;
if(document.getElementById('app')){
ReactDOM.render(<App/>,document.getElementById('app'));
}
In react-router-dom#6 the Routecomponent API changed significantly. Thechildrenprop is only used for rendering nestedRoutecomponents. All routed content is now rendered on a singleelementprop taking aReactNode`, a.k.a. JSX.
Move the content from being wrapped into the element prop.
function App() {
return (
// BEM
<div className="app">
<Router>
<Routes>
<Route path="/search" element={<h1>This is the search page</h1>} />
<Route path="/" element={<Home />} />
</Routes>
</Router>
</div>
);
}
Route component expects 2 parameters path, and element.
Refer to the official doc here
function App() {
return(
<BrowserRouter>
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/search" element={<Search/>} />
<Routes>
</BrowserRouter>
);
}
Unable to see the header text returned in my Home and Tutorials page in my react hooks site. Below is my App.js, Navigation.js and Home.js. Could someone please advise about the problem here.
App.js
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import './App.css';
import Home from "./components/Home";
import Tutorials from "./components/Tutorials";
import Navigation from './components/Navigation';
function App() {
return (
<BrowserRouter>
<Navigation>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</Navigation>
</BrowserRouter>
);
};
export default App;
Navigation.js
import React from 'react';
import { NavLink} from 'react-router-dom';
const Navigation = () => {
return (
<div className="App">
<div className="wrapper">
<div id="wrap">
<nav className="siteNavigation_nav_links">
<div className="row" ></div>
<div className="main_links_nav">
<NavLink className="mob_link" to="/">Home</NavLink>
<NavLink className="mob_link" to="/tutorials">Tutorials</NavLink>
</div>
</nav>
</div>
</div>
</div>
)
}
export default Navigation;
Home.js
import React from 'react';
const Home = () =>{
return (
<div className="App">
<h1>This is Home Page !</h1>
</div>
)
}
export default Home;
Tutorials.js
import React from 'react';
const Tutorials = () =>{
return (
<div><h1>This is Tutorials Page !</h1></div>
)
};
export default Tutorials;
Navigation is a Navbar component. And you want it everywhere on the site.
So, you can easily do it by using it outside the scope of the Switch component.
In doing so, it will render all over your screens.
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</BrowserRouter>
It might sound weird but when i go to inner page from homepage i the hover on link hover effect works fine and goes to innerpage. then in innerpage I have a back button which returns back to Homepage and after coming back to Homepage, when I hover on the link, no Hover effect.
this is how i have called the inner pages, if it helps
import React from 'react';
import { Switch , Route , BrowserRouter } from 'react-router-dom';
import Training from './section/training';
function App() {
return (
<div className="main homepage">
<BrowserRouter>
<div>
<Switch>
<Route path="/training" component={Training} exact={true} />
</Switch>
</div>
</BrowserRouter>
</div>
);
}
export default App;
Update
This is a part of main_content.js in which i have used <Link>
<div className="portfolio-grid-img">
<Link to="/training">
<img src={training_thumb} alt="Training thumbnail" />
<div className="portfolio-grid-overlay">
<h3>Site Revamp</h3>
<p>Training Terminal</p>
</div>
</Link>
</div>
You will need a route to your home page component
import React from 'react';
import { Switch , Route , BrowserRouter } from 'react-router-dom';
import Training from './section/training';
import Home from './homePageContent';
function App() {
return (
<BrowserRouter>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/training" component={Training} exact={true} />
</Switch>
</BrowserRouter>
);
}
export default App;