I'm fairly new to React and I'm trying to implement Navbar with react-bootstrap. I have the navigation bar set up but its not actually linking to any of my pages.
I've tried looking through the documentation but there's not much information there. I've looked at other posts but they all seem to be using older versions of react-bootstrap or not using it at all.
import React from "react";
import ReactDOM from "react-dom";
import "./components/stylesheets/index.css";
import * as serviceWorker from "./serviceWorker";
import "bootstrap/dist/css/bootstrap.min.css";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));
serviceWorker.unregister();
import React, { Component } from "react";
import "./components/stylesheets/App.css";
import NavBar from "./components/NavBar";
import Home from "./components/Home";
import About from "./components/About";
class App extends Component {
render() {
return <NavBar />;
}
}
export default App;
import React, { Component } from "react";
import { Navbar, Nav, Form, FormControl, Button } from "react-bootstrap";
import "./stylesheets/NavBar.css";
class NavBar extends Component {
state = {};
render() {
return (
<div id="bar">
<Navbar bg="light" variant="light">
<Navbar.Brand href="#home">CALC-U</Navbar.Brand>
<Nav className="ml-auto">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#about">About</Nav.Link>
<Nav.Link href="#updates">Updates</Nav.Link>
<Nav.Link href="#profile">Profile</Nav.Link>
</Nav>
</Navbar>
</div>
);
}
}
export default NavBar;
I want the app to open up to the Home page automatically, and then open up the other pages if the button is clicked on in the navBar.
Edit: I've created a working example, you can check the code: Codesandbox
Seems like you're not using react-router for handling the links.
First you need to wrap your component with a BrowserRouter like so:
import { BrowserRouter } from "react-router-dom";
import App from "./App";
const app = (
<BrowserRouter>
<App />
</BrowserRouter>
);
ReactDOM.render(app, document.getElementById("root"));
And in NavBar.js, inside a <Switch /> component, you declare the routes:
import { Switch, Route, Link } from "react-router-dom";
//(...)
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
In your case, you case use Link component inside your Nav.Link:
<Nav className="ml-auto">
<Nav.Link as={Link} to="/">Home</Nav.Link>
<Nav.Link as={Link} to="/about">About</Nav.Link>
</Nav>
Same applies if you want to add more links to the NavBar.
Sample application using react-bootstrap for Navbar
App.js
function App() {
return (
<div>
<Router>
<NavBar />
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" exact component={About} />
<Route path="/updates" exact component={Updates} />
<Route path="/profile" exact component={Profile} />
</Switch>
</Router>
</div>
);
}
const Home = () => <h1>Home</h1>;
const About = () => <h1>About</h1>;
const Updates = () => <h1>Updates</h1>;
const Profile = () => <h1>Profile</h1>;
NavBar.js
class NavBar extends React.Component {
state = {};
render() {
return (
<div id="bar">
<Navbar bg="light" variant="light">
<Navbar.Brand href="/">CALC-U</Navbar.Brand>
<Nav className="ml-auto">
<Nav.Link href="/">Home</Nav.Link>
<Nav.Link href="/about">About</Nav.Link>
<Nav.Link href="/updates">Updates</Nav.Link>
<Nav.Link href="/profile">Profile</Nav.Link>
</Nav>
</Navbar>
</div>
);
}
}
Related
I'm a beginner front-end dev and I try to make an imaginary website. I'm learning to react router now. So when I try to click on the home/about or contact button, It doesn't open it. What am I doing wrong?
here's my code below :
my App code :
import React from 'react';
import './App.css';
import Navibar from './Components/Navibar';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Home from './Components/Home';
import About from './Components/About';
import Contact from './Components/Contact';
let App = () => {
return (
<Router>
<div className="App">
<Navibar/>
<div className="content">
<Switch>
<Route exact path="/">
<Home/>
</Route>
<Route exact path="/About">
<About/>
</Route>
<Route exact path="Contact">
<Contact/>
</Route>
</Switch>
</div>
</div>
</Router>
);
}
export default App;
and here's my navbar code :
import React from 'react';
import {Nav, Navbar, Container} from 'react-bootstrap';
import {Link} from 'react-router-dom';
let Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand href="#home">My Portfolio</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link>
<Link to="#about">About me</Link>
<Link to="#contact">Contact</Link>
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
and here's my Home (the home/about and contact codes are the same) code :
import React from 'react';
let Home = () => {
return (
<>
<h2>Home</h2>
</>
);
}
export default Home;
I need help from a professional, because I'm getting stuck in this :)
The Navibar should link to the route paths you are rendering, not anchor tags (hashes) on the current page. The bootstrap Navbar.Brand component should also render a RRD Link component instead of a plain anchor tag with href attribute.
import React from 'react';
import { Nav, Navbar, Container} from 'react-bootstrap';
import { Link } from 'react-router-dom';
const Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand
as={Link} // <-- as a RRD Link component
to="/" // <-- to prop for Link component
>
My Portfolio
</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link> // <-- "/"
<Link to="/about">About me</Link> // <-- "/about"
<Link to="/contact">Contact</Link> // <-- "/contact"
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
The App component should specify route paths that match what the navbar is linking to.
Also the routes App is rendering within the Switch component should be rendered in inverse order of route path specificity. In other words, render the more specific paths prior to the less specific paths. This is to attempt to match and render more specific paths and falling back to less specific paths. This also effectively eliminates the need to use the exact prop.
const App = () => {
return (
<Router>
<div className="App">
<Navibar />
<div className="content">
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/contact">
<Contact />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</div>
</Router>
);
}
Hello cannot under stand how its work with this import and routes i created a navbar , created couples of pages like videolectures , so i create a router (link) to the videolectures page but then i press from main page there is a navbar displayed on VideoLecture i get white screen and its nothing going on display NavBar is gone i also try to write some code inside a VideoLecture page but its still not going to dislpay anyway CodeBelow
NavBar.js
import React from 'react';
import {Navbar,Nav,Container} from 'react-bootstrap';
import {Routes, Route, Link} from "react-router-dom";
import logo from '../img/logo1.jpg';
import Videolectures from '../pages/videolectures'
export default function NavBar() {
return(
<div>
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
<Container>
<img
src= { logo }
width="35"
height="35"
className="d-inline-block align-top rounded-circle"
alt = ""
href= "#home"
/>
<Navbar.Brand>React-Bootstrap</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="me-auto">
<Nav.Link as={Link} to="/videolectures">VideoLectures</Nav.Link>
<Nav.Link href="#pricing">Questions</Nav.Link>
</Nav>
<Nav>
<Nav.Link href="#deets">Login</Nav.Link>
<Nav.Link eventKey={2} href="#memes">
Sing Up
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
<Routes>
<Route path="/videolectures" element={<Videolectures/>}/>
</Routes>
</div>
);
}
app.js
import NavBar from './components/navbar';
import React from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
function App() {
return (
<NavBar/>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'bootstrap/dist/css/bootstrap.min.css';
import {BrowserRouter,Route,Routes} from 'react-router-dom';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={ <App/> }>
</Route>
</Routes>
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
VideoLecture.js
import React from 'react';
import {Card,Button} from 'react-bootstrap';
export default function Videolectures() {
return(
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
);
}
I also get messsage from web console
index.tsx:25 You rendered descendant <Routes> (or called `useRoutes()`) at "/" (under <Route path="/">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
Please change the parent <Route path="/"> to <Route path="*">.
warning # index.tsx:25
index.tsx:25 No routes matched location "/videolectures"
Try to move the BrowserRouter initialization in the App component and add to it also the /videolectures Route.
Also change the / route element to NavBar:
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<NavBar />}></Route>
<Route path='/videolectures' element={<Videolectures />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Of course, you will need to change the index.js back to
ReactDOM.render(
<React.StrictMode>
<App/>
</React.StrictMode>,
document.getElementById('root')
);
I'm trying to learn how to use GitHub Pages with a basic React project. But I'm having an issue with the URL of the different pages of the project (like /home /about...).
I have a GH Page here: https://naacho20.github.io/itis/
If u try to use the navbar, a 404 would appear, but thats wrong because I have a Router redirecting that. I show my code so I can explain better:
App.js:
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import { BrowserRouter, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import Home from "./pages/Home";
import About from "./pages/About";
import Portfolio from "./pages/Portfolio";
import Contact from "./pages/Contact";
export default function App() {
return (
<div>
<BrowserRouter>
<Navbar />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/portfolio" component={Portfolio} />
<Route path="/contact" component={Contact} />
</BrowserRouter>
</div>
);
}
Contact.js
import React from "react";
export default function Contact() {
return <div>Contact</div>;
}
Navbar.js
import React from "react";
import { withRouter } from "react-router-dom";
import { Navbar, Nav } from "react-bootstrap";
const menuItems = [
{
to: "/",
title: "Inicio",
},
{
to: "/about",
title: "Sobre mí",
},
{
to: "/portfolio",
title: "Portfolio",
},
{
to: "/contact",
title: "Contacto",
},
];
class NavBar extends React.Component {
getNavLinkClass = (path) => {
return this.props.location.pathname === path ? "active" : "";
};
render() {
return (
<div>
<Navbar bg="dark" expand="lg" variant="dark">
<Navbar.Brand href="/">IT.IS</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto">
{menuItems.map((item, index) => {
return (
<Nav.Link
key={index}
className={this.getNavLinkClass(item.to)}
href={item.to}
>
{item.title}
</Nav.Link>
);
})}
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
);
}
}
NavBar = withRouter(NavBar);
export default NavBar;
So I see thats a problem from the URL, I see when I start the app, this goes to localhost:3000/itis and the Home page don't render, but if I remove the itis (localhost:3000/) it render the Home Page. I don't know what I'm doing wrong, any suggestion?
Ty.
This is because you are using SPA app so your all routes serving from one html file. You should use HashRouter to fix it. It is so simple you should import HashRouter instead of
BrowserRouter
import { BrowserRouter} from "react-router-dom";
to
import { HashRouter } from "react-router-dom";
and wrap it with your App
Also you have to wrap your Routes with Switch like this
<BrowserRouter>
<Switch>
<Navbar />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/portfolio" component={Portfolio} />
<Route path="/contact" component={Contact} />
</Switch>
</BrowserRouter>
I took a break from my project for a day and today I come back to and suddenly i have an issue with my react router. I was messing around with adding more components but deleted them and now my Homepage, about, or gallery component dont render when I change the page with then navbar. The url still changes if i click on gallery to /gallery but the <h1>Gallery page</h1> does not show up anymore.
App.js
import React, { Component } from 'react';
import './App.css';
import NavigationBar from './components/navbar';
import About from './components/About';
import Home from './components/homepage';
import Gallery from './components/gallery';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
class App extends Component {
state = {};
render() {
return (
<Router>
<div className="App">
<NavigationBar />
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/gallery" component={Gallery} />
</Switch>
</div>
</Router>
);
}
}
export default App;
Homepage
import '../css/homepage.scss';
class Homepage extends Component {
state = {};
render() {
return (
<div>
<h1>Hello</h1>
</div>
);
}
}
export default Homepage;
About
class About extends Component {
state = {};
render() {
return <h1>About</h1>;
}
}
export default About;
Gallery
class gallery extends Component {
state = {};
render() {
return <h1>Gallery page</h1>;
}
}
export default gallery;
Nav code
import React, { Component } from 'react';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import { link } from 'react-router-dom';
import { NavLink } from 'react-router-dom';
import '../css/navbar.scss';
import $ from 'jquery';
import logo from '../images/logo.png';
class NavigationBar extends Component {
state = {};
render() {
return (
<Navbar fixed="top" className="navbarr" bg="light" expand="sm">
<Navbar.Brand
activeClassName="nav-link--active"
className="BrandLink"
href="/"
>
<img
alt=""
src={logo}
width="45"
height="30"
className="d-inline-block align-top"
/>
BRAND NAME HERE
</Navbar.Brand>
<Navbar.Toggle />
<Navbar.Collapse className="justify-content-end">
<Nav className="ml-auto">
<Nav.Link
className="NavLink"
activeClassName="nav-link--active"
href="/About"
>
About
</Nav.Link>
<Nav.Link
className="NavLink"
activeClassName="nav-link--active"
href="/Gallery"
>
Gallery
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default NavigationBar;
Try moving your default route to the end of routes. i.e.
import React, { Component } from 'react';
import './App.css';
import NavigationBar from './components/navbar';
import About from './components/About';
import Home from './components/homepage';
import Gallery from './components/gallery';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
class App extends Component {
state = {};
render() {
return (
<Fragment>
<div className="App">
<NavigationBar />
</div>
<Router>
<Switch>
<Route path="/about" component={About} />
<Route path="/gallery" component={Gallery} />
<Route path="/" component={Home} />
</Switch>
</Router>
</Fragment>
);
}
}
export default App;
The problem is with you Navbar components. Try this one:
import React, { Component } from "react";
import Navbar from "react-bootstrap/Navbar";
import Nav from "react-bootstrap/Nav";
import { Link } from "react-router-dom";
import "../css/navbar.scss";
import $ from "jquery";
import logo from "../images/logo.png";
class NavigationBar extends Component {
state = {};
render() {
return (
<Navbar fixed="top" className="navbarr" bg="light" expand="sm">
<Navbar.Brand activeClassName="nav-link--active" className="BrandLink">
<Link to="/">
<img
alt=""
src={logo}
width="45"
height="30"
className="d-inline-block align-top"
/>
BRAND NAME HERE
</Link>
</Navbar.Brand>
<Navbar.Toggle />
<Navbar.Collapse className="justify-content-end">
<Nav className="ml-auto">
<Nav.Link className="NavLink" activeClassName="nav-link--active">
<Link to="/about">About</Link>
</Nav.Link>
<Nav.Link className="NavLink" activeClassName="nav-link--active">
<Link to="/gallery">Gallery</Link>
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default NavigationBar;
I'm using react-router v.4 in my React app and have faced with this trouble: when I click at <Link> component, the URL is changing, but data is not rendering.
Here is my code snippets:
Header.component.js:
import React from 'react';
import Navbar from 'react-bootstrap/lib/Navbar';
import Nav from 'react-bootstrap/lib/Nav';
import NavItem from 'react-bootstrap/lib/NavItem';
import NavDropdown from 'react-bootstrap/lib/NavDropdown';
import MenuItem from 'react-bootstrap/lib/MenuItem';
import {Link} from 'react-router-dom';
//utils
import History from '../../utils/History';
import bindAll from 'lodash/bindAll';
class Header extends React.Component {
constructor(props) {
super(props);
bindAll(this, '_getHeaderItem');
}
_getHeaderItem() {
switch (History.location.pathname) {
case '/sign-in':
return <li><Link to='/sign-up'>Sign Up</Link></li>;
break;
case '/sign-up':
return <li><Link to='/sign-in'>Sign In</Link></li>;
break;
default:
return null;
}
}
render() {
return (
<Navbar collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
Elevator
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#">Link</NavItem>
</Nav>
<Nav pullRight>
{this._getHeaderItem()}
<NavDropdown eventKey={4} id='member-dropdown' title={
<div className='inline-block'>
<img src='' alt='' />
Username
</div>
}>
<MenuItem eventKey={4.1}>My profile</MenuItem>
<MenuItem eventKey={4.2}>Logout</MenuItem>
<MenuItem divider />
<MenuItem eventKey={4.3}>Administration</MenuItem>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default Header;
app.js: (which is collecting all components and has routing logic)
import React from 'react';
import ReactDOM from 'react-dom';
import fetch from 'whatwg-fetch';
import { Router, Route, Redirect } from 'react-router';
//utils
import History from './utils/History';
//components
import App from './common/components/App.component';
import SignUp from './modules/signUp/SignUp.component';
import SignIn from './modules/signIn/SignIn.component';
import Footer from './modules/footer/Footer.component';
//styles
require ('./../styles/main.scss');
let loggedIn = localStorage.getItem('token');
loggedIn ? History.replace('/dashboard') : History.replace('/sign-in');
ReactDOM.render(
<Router history={History}>
<App>
<Route path='/sign-in' render={() => (
loggedIn ? (
<Redirect to="/dashboard" />
) : (
<SignIn />
)
)} />
<Route path='/sign-up' render={() => (
loggedIn ? (
<Redirect to="/dashboard" />
) : (
<SignUp />
)
)} />
</App>
</Router>,
document.getElementById('app'));
ReactDOM.render(<Footer />, document.getElementById('footer'));
Temporary, I've done it with click handling (prevent default behavior and window.location.reload('some/path')), but it's not a React way.
I'll appreciate any advice.