Once i added browserRouter it is showing me an empty page - reactjs

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>

Related

Issue with BrowserRouter - navigation doesn't work properly

This is my first reactjs application and I have an issue with the navigation. When I try to navigate via url .../posts or ../home only the main App page is showing up. How can I fix this? Thank you in advance!
I have App main page where user can Login or Register:
import React,{useState, useEffect} from 'react';
import {Register} from './subcomponents/Register';
import {Login} from './subcomponents/Login';
export function App() {
return (
<div className="root">
<Login />
<p>Hi {name}</p>
<Register/>
</div>
);
}
Home page, when the user is logged in to be redirected:
import React from 'react';
import { Posts } from './Posts';
import {Home} from './Home.jsx';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import {Nav} from './Nav';
export function Home(){
return(
<div id="home">
<BrowserRouter>
<Nav/>
<Routes>
<Route path='/home' element={<Home/>}/>
<Route path="/posts" element={<Posts/>}/>
</Routes>
</BrowserRouter>
<h3>This is the home page</h3>
</div>
)
}
Nav component:
import React from 'react';
import {Link} from 'react-router-dom'
import '../css/nav.css';
import Logo from '../images/Logo.png';
export function Nav(){
return(
<div id="nav">
<img src={Logo} className="nav-logo" alt="logo"/>
<ul>
<li>
<Link to="/home">Home</Link>
</li>
<li>
<Link to="/posts">Posts</Link>
</li>
</ul>
</div>
)
}
for access to components in the project by url,it need be to render before.
export function App(){
return(
<BrowserRouter>
<div id="app">
<Routes>
<Route path='/home' element={<Home/>}/>
<Route path="/posts" element={<Posts/>}/>
</Routes>
</div>
</BrowserRouter>
)
}

Unable to see the text returned from home and tutorials page in react-hooks site

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>

How can I redirect users to different pages with Link

I have the code like bellow. I want when I click in the link it should direct me to the other page.
But It doesn't do anything. Could someone please help with this?
<Link className="portfolio-link" data-toggle="modal" to={`/blog/${this.props.post.slug}`}>
<div className="portfolio-hover">
<div className="portfolio-hover-content">
<i className="fas fa-plus fa-3x"></i>
</div>
</div>
{this.props.post.PostImage ? this.props.post.PostImage.length > 0 ? <img style={{width: '100%'}} className="img-fluid" src={API.makeFileURL(this.props.post.PostImage[0].thumbnail, null)} alt="/"/>
: null: null}
</Link>
You may create separate component for navigation purpose where you may put all of your url links
NavBar component
import React from "react";
import { Link } from "react-router-dom";
const NavBar = ({ menuData }) => {
return (
<nav>
<ul>
{menuData.map(menu => {
return (
<li>
<Link to={menu.path}>
{menu.name}
</Link>
</li>
);
})}
</ul>
</nav>
);
};
export default NavBar;
You have to ensure the route section, this section specially manage your route and it will redirect you to your desired page/component based on url change
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/" component={Home} />
</Switch>
So I keep the NavBar and the route closely in App.js
App.js
import React, { Component } from "react";
import NavBar from "./navbar";
import { Route, Redirect, Switch } from "react-router-dom";
import "./App.css";
class App extends Component {
state = {
navBars: [
{ name: "Home", path: "/" },
{ name: "Login", path: "/login" }
]
};
render() {
return (
<React.Fragment>
<main>
<NavBar
menuData={this.state.navBars}
></NavBar>
<div className="content">
<Switch>
<Route exact path="/login" component={Login} />
<Route exact path="/" component={Home} />
</Switch>
</div>
</main>
</React.Fragment>
);
}
}
export default App;
And finally you have to make sure your navbar/Link and Routes should be under Router, So BrowserRouter confirm this thing
<BrowserRouter>
<App />
</BrowserRouter>
You may have a look in my index.js
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter} from "react-router-dom";
import "./index.css";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
Hope it will help you

Hover not working after returning from inner page to homepage

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;

ReactJS - I can't reach a component inside in other by route

I have an website with this structure:
<Header>
<MenuPrincipal>
<Home></Home>
</MenuPrincipal>
</Header>
When I click on a logo in the Header, it isn't working, nothing is happening and I would like to access the Home component The <Home> component is inside of <MenuPrincipal>. The <Header> component is the parent component, how you can see in the structure above. How can I solve it?
import React from 'react'
import { BrowserRouter as Router, Route, Link, } from 'react-router-dom'
import Home from './Home';
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import { showBGImage } from './actions';
const Header = (props) => {
return (
<div className="header">
<div>
<Router>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Router>
</div>
</div>
)
}
export default Header
<BrowserRouter> it's more like a history browser, you need to use Switch instead.
import { BrowserRouter as Router, Route, Link, Switch} from 'react-router-dom'
const Header = (props) => {
return (
<div className="header">
<Router>
<Switch>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Switch>
</Router>
</div>
)
}

Resources