I am building an application that has different pages and I was wondering how I would be able to restructure my routess i want to load login component on '/' and i have this.props.history.push("/homepage") on login page which open up the homepage on successful login i have a navbar in the homepage with different links would i address loading the components in the homepage when links are clicked and i want the navbar to stay only in the homepage and load component next to it.I am new to react.Whats the best solution to this kind of scenario.
navbar code
import React, { Component } from "react";
import jwt_decode from "jwt-decode";
import { Link, withRouter } from "react-router-dom";
class Navbar extends Component {
logOut(event) {
event.preventDefault();
localStorage.removeItem("usertoken");
this.props.history.push("/login");
}
render() {
return (
<ul style={navBarStyle} id="sidenav-1" className="sidenav sidenav-fixed">
<li>
<h1 className="center-align" style={{ fontSize: "30px" }}>
Hello
</h1>
</li>
<li>
<Link to="/profile">
<i style={linkStyle} className="material-icons ">
person
</i>
</Link>
</li>
<li>
<a href="https://twitter.com/MaterializeCSS" target="_blank">
<i style={linkStyle} className="material-icons ">
book
</i>
</a>
</li>
<li>
<Link to="/searchcourse">
<i style={linkStyle} className="material-icons">
search
</i>
</Link>
</li>
<li style={linkStyle}>
<a href="" onClick={this.logOut.bind(this)}>
<i style={linkStyles} className="fas fa-sign-out-alt" />
</a>
</li>
</ul>
);
}
}
homepage
class Homepage extends Component {
componentDidMount() {
document.title = "Dashboard";
}
render() {
return (
<div>
<Navbar />
<div className="container">
<div>
<h3>Dashboard</h3>
<hr />
</div>
<div className="col" />
</div>
</div>
);
}
}
this is my app.js
<BrowserRouter>
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
<Route path="/homepage" component={Homepage} />
<Route exact path="/profile" component={Profile} />
<Route exact path="/searchcourse" component={SearchHelper} />
<Route exact path="/item" component={CourseItem} />
</BrowserRouter>
You can use nested routes. Keep Homepage specific sub-routes inside Homepage component. That way route /homepage will open up Homepage Component.
Inside Homepage you can have Navbar and the child routes of homepage like :
/homepage/profile, /homepage/item etc.
Here is one article to dig deep: https://medium.com/#marxlow/simple-nested-routes-with-layouts-in-react-router-v4-59b8b63a1184
Related
my live site and my code
all my links would just fine on npm start but when I ran npm run build the routes for some pages stop working. /about doesn't work at all and if you are on the 404 page and navigate back the /projects page stops working as well.
You should use BrowserRouter to wrap all your Routes in your Nav component and use Link to render About component as follows.
For more info: https://reactrouter.com/web/guides/quick-start
import "./nav.css";
import {
BrowserRouter as Router,
Route,
Link,
Redirect,
} from "react-router-dom";
import LogoS from "../imgs/logo-sm.png";
import Logo1 from "../imgs/logo-01.png";
import Projects from "./project-list";
import Aub from "./projects/auburn";
import M3d from "./projects/m3d";
import Uroute from "./projects/uroute";
import Thesis from "./projects/thesis";
import About from "./about";
import ACNH from "./projects/acnh";
const Nav = () => {
return (
<Router>
<div>
<header className="navbar">
<div className="container">
<Link to="/projects">
<p className="mb-0 navbar-brand">
<img
alt="Trisha Dring"
className="image logo d-sm-none"
src={LogoS}
/>
<img
className="image logo d-none d-sm-block"
alt="Trisha Dring"
src={Logo1}
/>
</p>
</Link>
<ul className="nav">
<li className="nav-item">
<Link
to="/about"
className="nav-link active"
aria-current="page"
>
About
</Link>
</li>
<li className="nav-item">
<a className="nav-link" href="/">
Resume
</a>
</li>
<li className="nav-item">
<a
className="nav-link"
href="mailto:trisha.dring+website#gmail.com"
>
Contact
</a>
</li>
</ul>
</div>
</header>
<Route path="/projects" component={Projects} />
<Route path="/ACNH" component={ACNH} />
<Route path="/Auburn" component={Aub} />
<Route path="/about" component={About} />
<Route path="/M3D" component={M3d} />
<Route path="/Uroute" component={Uroute} />
<Route path="/FixHFA" component={Thesis} />
<Route exact path="/">
<Redirect to="/projects" />
</Route>
</div>
</Router>
);
};
export default Nav;
By the way, in your About component, use className instead of class as follows. Otherwise, it will give warnings.
<p className="explain">Hi. I'm Trisha</p>
I am having a unusual problem in my routing. I have a home page and then in the mid level of the home page, I have a link of my about page. When I click on that in redirects me to the mid level of the about page. Why it is not taking me to the top level of the about page. I think I have scrolled in home page cause it has also scrolled in the about page I don't know it is a very weird problem .I am providing my nav items and all my Links' code here. Please give me a solution if you can.
import React, { Component } from 'react'
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom'
import Nav from './Components/Nav'
import Home from './Components/Home'
import About from './Components/About'
import Project from './Components/Project'
import Skills from './Components/Skills'
import Services from './Components/Services'
import Contact from './Components/Contact'
const App = () => {
return (
<>
<BrowserRouter>
<Nav />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/service" component={Services} />
<Route path="/project" component={Project} />
<Route path="/skill" component={Skills} />
<Route path="/contact" component={Contact} />
<Redirect to="/" />
</Switch>
</BrowserRouter>
</>
)
}
export default App
import React, { useState } from 'react'
import { NavLink, Link } from 'react-router-dom'
import Mode from './Mode'
import Button from '#material-ui/core/Button'
const Nav = () => {
const [icon, setIcon] = useState(false)
const [open, setClose] = useState(false)
function Nav() {
document.querySelector('#NavLinks').style.transform = 'scaleX(1)'
setClose(!open)
setIcon(!icon)
}
function NavClose() {
document.querySelector('#NavLinks').style.transform = 'scaleX(0)'
setClose(!open)
setIcon(!icon)
}
return (
<>
<nav data-aos="fade-in">
<div id="Container">
<div id="NavContentWrapper">
<h2 id="NavTitle">
<Link to="/">DevR</Link>
</h2>
<ul id="NavLinks">
<li>
<NavLink exact activeClassName="active" to="/">
Home
</NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/about">
About
</NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/service">
Service
</NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/project">
Projects
</NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/skill">
Skills
</NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/contact">
Contact
</NavLink>
</li>
<li>
<Mode />
</li>
</ul>
<div id="Bar">
<Button
variant="outlined"
color="primary"
onClick={open ? NavClose : Nav}
>
{icon ? (
<i className="fas fa-times"></i>
) : (
<i className="fas fa-bars"></i>
)}
</Button>
</div>
</div>
</div>
</nav>
</>
)
}
export default Nav
Add this to your App component:
const location = useLocation();
useLayoutEffect(() => {
window.scrollTo(0, 0);
}, [location.pathname]);
I'm trying to render a component inside my dashboard component by clicking a Link in that component,
I need to render Home or Contacts or About component inside my div
should I use array to store the components I want to render or route? adn how do I do it?
import React, { useContext, useEffect, useState, Fragment } from "react";
import { BrowserRouter as Router, Route, Switch, Link } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Contacts from "./Contacts";
const DashBoard = () => {
const authContext = useContext(AuthContext);
return (
<div>
<div> // this is my side nav
<Route>
<ul>
<li>
<Link to="/dashboard/home">Home</Link>
</li>
<li>
<Link to="/">Issues</Link>
</li>
<li>
<Link to="/">Contacts</Link>
</li>
<li>
<Link to="/dashBoard/about">About</Link>
</li>
</ul>
</Route>
</div>
<div>
<nav>
<button
className="btn btn-primary"
id="menu-toggle"
onClick={() => setIsToggled(!isToggled)}
>
Toggle Menu
</button>
</nav>
<div className="container-fluid">
// i need to render a component here ex.) <Home/> | <Contacts/> | <About/> when a Link is clicked!
</div>
</div>
</div>
);
};
export default DashBoard;
in my app.js
<Fragment>
<Navbar />
<div className="">
<Switch>
<Route exact path="/dashBoard" component={DashBoard} />
<Route exact path="/login" component={Login} />
</Switch>
</div>
</Fragment>
i'm trying to implement router to my reactjs app and after setting things up, my page wont even load, it just keeps reloading in my browser till it crashs.
This is my app.js
import React, { Component } from "react";
import { BrowserRouter, Route } from "react-router-dom";
class App extends Component {
render() {
return (
<div className="App">
<Header />
<Content
job={this.state.header_infos[0].job}
college={this.state.header_infos[0].college}
/>
<Skills />
<Portfolio />
<Timeline />
<Footer />
<BrowserRouter>
<Route exact path="/" component={App} /> /* if it clicks in my logo, redirect to mainpage */
<Route path="/monitoria" component={Monitoria} /> /* if clicks in Monitoria from Navbar, redirects to Monitoria component. */
</BrowserRouter>
</div>
);
}
}
And this is my Header.js with my navbar
import React from "react";
import "../sass/Header.scss";
import { NavLink } from "react-router-dom";
const Header = () => {
return (
<header className="">
<NavLink exact to="/">
<h1 className="logo">Logo</h1>
</NavLink>
<input type="checkbox" id="nav-toggle" className="nav-toggle" />
<nav>
<ul>
<li className="menu-item">
<NavLink to="/monitoria">Monitoria</NavLink>
</li>
<a className="btn-rounded" href="#">
<li className="menu-item">Fale comigo</li>
</a>
</ul>
</nav>
<label htmlFor="nav-toggle" className="nav-toggle-label">
<span />
</label>
</header>
);
};
export default Header;
If i remove React Router DOM from my App.js my page works just fine. What i'm doing wrong?
As mentioned in comment, in app.js, you are assigning class App into a route that is contained in the App class.
I'm using react-router v4 for routing in React. The issues i'm facing is that the react-router changes the location but is unable to render the component. I heard that using redux with react blocks updates but i'm not using redux here .
Sidebar.js
import React, {Component} from 'react';
import {
BrowserRouter as Router,
Link
} from 'react-router-dom';
import $ from 'jquery';
import {withRouter} from 'react-router-dom';
class Sidebar extends Component {
render () {
return (
<Router>
<div>
<button className="navbar-toggler" data-toggle="collapse" data-target="#pill">
<span><i className="fa fa-ellipsis-v"></i> menu</span>
</button>
<div id="pill">
<ul className="nav nav-pills flex-column sidebar">
<li className="nav-item">
<Link to="/" className="nav-link">
<i className="fa fa-dashboard"></i> Dashboard
</Link>
</li>
<li className="nav-item">
<a href="#students" className="nav-link" data-toggle="collapse">
<i className="fa fa-users"></i> Students
</a>
<ul className="collapse" id="students" style={{marginLeft: '-25px'}}>
<li className="nav-item"><Link to="/students" className="nav-link"><i className="fa fa-eye"></i> View students</Link></li>
</ul>
</li>
</ul>
</div>
</div>
</Router>
)
}
}
export default withRouter(Sidebar);
App.js
import React, { Component } from 'react';
import Navbar from './Navbar';
import Sidebar from './Sidebar';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import Dashboard from './Dashboard';
import Students from './Students';
class App extends Component {
render() {
const { match } = this.props;
return (
<Router>
<div>
<Navbar />
<div className="container-fluid main">
<div className="row">
<div className="col-sm-2 area-left">
<Sidebar />
</div>
<div className="col-sm-10 area-right float-right">
<div>
<Route exact path="/" component={Dashboard} />
<Route exact path="/students" component={Students} />
</div>
</div>
</div>
</div>
</div>
</Router>
);
}
}
export default App;
When route is /
When route is /students
Reloading the page renders the component
The Link you specified in your sidebar is trying to use the Router you wrapped the component in, not the Router you defined your links in.
This is what your sidebar component should look like.
import React, {Component} from 'react';
import {
Link,
withRouter
} from 'react-router-dom';
import $ from 'jquery';
class Sidebar extends Component {
render () {
return (
<div>
<div>
<button className="navbar-toggler" data-toggle="collapse" data-target="#pill">
<span><i className="fa fa-ellipsis-v"></i> menu</span>
</button>
<div id="pill">
<ul className="nav nav-pills flex-column sidebar">
<li className="nav-item">
<Link to="/" className="nav-link">
<i className="fa fa-dashboard"></i> Dashboard
</Link>
</li>
<li className="nav-item">
<a href="#students" className="nav-link" data-toggle="collapse">
<i className="fa fa-users"></i> Students
</a>
<ul className="collapse" id="students" style={{marginLeft: '-25px'}}>
<li className="nav-item"><Link to="/students" className="nav-link"><i className="fa fa-eye"></i> View students</Link></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
)
}
}
export default withRouter(Sidebar);
More explanation:
You should only wrap your top level component in the router since it for lack of better terms spawns all the children in your app. Furthermore, it will automatically inject the props for the router unless you are using redux. So unless you're using redux no need for withRouter() *.
*lmk if i'm wrong, i'm not an expert.
Example of your top level app component:
render() {
return(
<Router history={History}>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/student" component={Student} />
</Switch>
</Router>
)
}
Essentially wrap your Route with Switch.
Update App.js to the below
import React, { Component } from 'react';
import Navbar from './Navbar';
import Sidebar from './Sidebar';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Dashboard from './Dashboard';
import Students from './Students';
class App extends Component {
render() {
const { match } = this.props;
return (
<Router>
<div>
<Navbar />
<div className="container-fluid main">
<div className="row">
<div className="col-sm-2 area-left">
<Sidebar />
</div>
<div className="col-sm-10 area-right float-right">
<Switch>
<Route exact path="/" component={Dashboard} />
<Route exact path="/students" component={Students} />
</Switch>
</div>
</div>
</div>
</div>
</Router>
);
}
}
export default App;