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]);
Related
I am new in react facing some issues in hooks can't able to resolve the problem. Can anybody help me.
react.development.js:207 Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
You might have mismatching versions of React and the renderer (such as React DOM)
You might be breaking the Rules of Hooks
You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem
App.js
import NavBar from "./components/NavBar";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import { Home } from "./components/Pages/Home";
import { About } from "./components/Pages/About";
import { Blog } from "./components/Pages/Blog";
import { Contact } from "./components/Pages/Contact";
function App() {
return (
<Router>
<NavBar />
<div className="pages">
<Routes>
<Route exact path="/" element={<Home/>} />
<Route path="/about" element={<About/>} />
<Route path="/blog" element={<Blog/>} />
<Route path="/contact" element={<Contact/>} />
</Routes>
</div>
</Router>
);
}
export default App;
ERROR
enter image description here
Navbar.js
import React, { useState } from "react";
import { NavLink } from "react-router-dom";
import "./NavBar.css";
NavBar()
export default function NavBar() {
const [click, setClick] = useState(false);
const handleClick = () => setClick(!click);
return (
<>
<nav className="navbar">
<div className="nav-container">
<NavLink exact to="/" className="nav-logo">
CodeBucks
<i className="fas fa-code"></i>
</NavLink>
<ul className={click ? "nav-menu active" : "nav-menu"}>
<li className="nav-item">
<NavLink
exact
to="/"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Home
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/about"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
About
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/blog"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Blog
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/contact"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Contact Us
</NavLink>
</li>
</ul>
<div className="nav-icon" onClick={handleClick}>
<i className={click ? "fas fa-times" : "fas fa-bars"}></i>
</div>
</div>
</nav>
</>
);
}
if i remove navbar() from navbar.js more errors are showing
enter image description here
package.json
enter image description here
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
// import './index.css';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<App />
);
You called NavBar() inside your file.
NavBar()
export default function NavBar() {
You are calling the component like the general function in the Navbar.js.
import React, { useState } from "react";
import { NavLink } from "react-router-dom";
import "./NavBar.css";
[enter image description here][1]
// Please remove this line
// NavBar()
export default function NavBar() {
const [click, setClick] = useState(false);
const handleClick = () => setClick(!click);
return (
<>
<nav className="navbar">
<div className="nav-container">
<NavLink exact to="/" className="nav-logo">
CodeBucks
<i className="fas fa-code"></i>
</NavLink>
<ul className={click ? "nav-menu active" : "nav-menu"}>
<li className="nav-item">
<NavLink
exact
to="/"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Home
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/about"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
About
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/blog"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Blog
</NavLink>
</li>
<li className="nav-item">
<NavLink
exact
to="/contact"
activeClassName="active"
className="nav-links"
onClick={handleClick}
>
Contact Us
</NavLink>
</li>
</ul>
<div className="nav-icon" onClick={handleClick}>
<i className={click ? "fas fa-times" : "fas fa-bars"}></i>
</div>
</div>
</nav>
</>
);
}
React Component should not be called like a function. That should settled inside of DOM element.
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>
When I click on "blog" I would like to open only blog section on the whole page, not the extra blog component together with other components on main page. When I click now on Blog, it opens after team component on the same page. How can I change it and open as a whole page?
import React from 'react';
import './App.css';
import { BrowserRouter, Route} from "react-router-dom";
import Header from './components/Header';
import Home from './components/Home';
import About from './components/About';
import Team from './components/Team';
import Blog from './components/Blog';
import Contact from './components/Contact';
import ScrollToTop from './components/ScrollToTop';
const App = () => {
return (
<BrowserRouter>
<Header />
<Home />
<About />
<Team />
<Route path="/blog" component={Blog}></Route>
<Contact />
<ScrollToTop />
</BrowserRouter>
);
}
export default App;
This is my another component:
import React from 'react';
import { Link as Link1} from 'react-scroll';
import { Link as Link2 } from 'react-router-dom';
import styled from 'styled-components';
const LeftNav = ({ open }) => {
return (
<NavMenu open={open}>
<ul>
<li>
<Link1
smooth={true}
spy={true}
offset={0}
duration={1000}
to="home"
>Home</Link1>
</li>
<li>
<Link1
activeClass="active"
smooth={true}
spy={true}
offset={0}
duration={1200}
to="about"
>About</Link1>
</li>
<li>
<Link1
activeClass="active"
smooth={true}
spy={true}
offset={0}
duration={1200}
to="team"
>Team</Link1>
</li>
<li>
<Link2 id="blog"
to="/blog"
target="_blank"
>Blog</Link2>
</li>
<li>
<Link1
activeClass="active"
smooth={true}
spy={true}
offset={0}
duration={1200}
to="contact"
>Contact</Link1>
</li>
</ul>
</NavMenu>
)
}
export default LeftNav
````
You are always rendering the Header, Home, About, etc because they are not inside of a <Route/> component under <BrowserRouter/>. As in the your Blog component is being rendered only when you are on the /blog route, but the rest of your components are always being shown since there is no <Route/> wrapping them. You should put your main page in one route and your blog page under another, for example:
const App = () => {
return (
<BrowserRouter>
<Route path="/home"> {/* <-- insert the right path for your main page here*/}
<Header />
<Home />
<About />
<Team />
<Contact />
<ScrollToTop />
</Route>
<Route path="/blog" component={Blog}></Route>
</BrowserRouter>
);
}
In the example above you should see the main page only when you go to {base_url}/home, so you will have to configure it to your needs if you need something different. Take a look at the Route documentation on its path prop to properly set it up.
I tried to do sample login page in react using react hooks(useState()).
When user is not login I redirect it to home. When I use hooks, that is not being redirected.
here is my code
import React, {useState} from 'react';
import {BrowserRouter as Router, Route, NavLink, Redirect} from 'react-router-dom';
import './App.css';
function App() {
const [isLogin, changeLogin] = useState(false);
return (
<Router>
<div>
<ul>
<li>
<NavLink to="/" exact activeStyle={{'color':'green'}}>Home</NavLink>
</li>
<li>
<NavLink to="/about" activeStyle={{'color':'green'}}>About</NavLink>
</li>
<li>
<NavLink to="/user/stack" activeStyle={{'color':'green'}}>User Stack</NavLink>
</li>
<li>
<NavLink to="/user/overflow" activeStyle={{'color':'green'}}>User Overflow</NavLink>
</li>
</ul>
<input className="loginButton" type="button" value={isLogin ? 'Logout' : 'Login'} onClick={ () => changeLogin(!isLogin)}/>
<hr/>
<Route path="/" exact strict render={() => <Home/>} />
<Route path="/about" exact strict render={() => <About/>} />
<Route path="/user/:username" exact strict render={({match}) => {
return(isLogin ? (<User username={match.params.username}/>) : (<Redirect to="/" />))
}} component={User} />
</div>
</Router>
);
}
function Home(){
return(
<div>
<h2>Home</h2>
</div>
)
}
function About(){
return(
<div>
<h2>About</h2>
</div>
)
}
const User = ({match}) => {
return(<h2>Welcome User {match.params.username}</h2>);
}
export default App;
I use usestate to handle the state which tells the user is logged in or not. How can I achieve this using react hooks?
Removing component={User} on your route should solve the problem :
<Route path="/user/:username" exact strict
render={({match}) => isLogin ?
<User username={match.params.username}/> :
<Redirect to="/" />
}
/>
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