react router not working on some pages on live site - reactjs

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>

Related

My React App shows a blank white screen on localhost:3000 with no errors

My App.js file is
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import "bootstrap/dist/css/bootstrap.min.css";
import CreateTodo from "./components/create-todo.component";
import EditTodo from "./components/edit-todo.component";
import TodosList from "./components/todos-list.component";
import logo from "./logo.png";
class App extends Component {
render() {
return (
<Router>
<div className="container">
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="https://codingthesmartway.com" target="_blank">
<img src={logo} width="30" height="30" alt="CodingTheSmartWay.com" />
</a>
<Link to="/" className="navbar-brand">MERN-Stack Todo App</Link>
<div className="collpase nav-collapse">
<ul className="navbar-nav mr-auto">
<li className="navbar-item">
<Link to="/" className="nav-link">Todos</Link>
</li>
<li className="navbar-item">
<Link to="/create" className="nav-link">Create Todo</Link>
</li>
</ul>
</div>
</nav>
<Route path="/" exact component={TodosList} />
<Route path="/edit/:id" component={EditTodo} />
<Route path="/create" component={CreateTodo} />
</div>
</Router>
);
}
}
export default App;
On my browser, all I can see is a blank white screen, thats it. There are just 3 more routes for the application: /, /create, and /edit/:id for which I have connected to three other components TodosList, EditTodo, CreateTodo.
You need to change few things
First import Routes
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
Then wrap your router code inside Routes
<Routes>
<Route path="/" exact component={TodosList} />
<Route path="/edit/:id" component={EditTodo} />
<Route path="/create" component={CreateTodo} />
</Routes>
For more info about react router check this link
https://reactrouter.com/docs/en/v6/getting-started/overview
I used to face the same problem. What my mistake was,
when I writing a tap my extension make close tag also.
like
In react,
we use only ""
The open tag and close tag is the problem for me.

Issue with react routing, unable to debug the code and find out the issue

Here is the App.js:
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import "bootstrap/dist/css/bootstrap.min.css";
import NavBar from "./components/navbar.component";
import ExercisesList from "./components/exercises-list.component";
import EditExercise from "./components/edit-exercise.component";
import CreateExercise from "./components/create-exercise.component";
import CreateUser from "./components/create-user.component";
function App() {
return (
<Router>
<div className="container">
<NavBar />
<br />
<Route path="/" component={ExercisesList} />
<Route path="/edit/:id" component={EditExercise} />
<Route path="/create" component={CreateExercise} />
<Route path="/create" component={CreateUser} />
</div>
</Router>
);
}
export default App;
here is Navbar component code:
import React, { Component } from "react";
import { Link } from "react-router-dom";
export default class Navbar extends Component {
render() {
return (
<nav className="navbar navbar-dark bg-dark never-expend-lg ">
<Link to="/" className="navbar-brand">
ExeriseTracker
</Link>
<div className="collpase navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="navbar-item">
<Link to="/" className="nav-link">
Exercises
</Link>
</li>
<li className="navbar-item">
<Link to="/create" className="nav-link">
Create Exercise Log
</Link>
</li>
<li className="navbar-iteam">
<Link to="/user" className="nav-link">
Create User
</Link>
</li>
</ul>
</div>
</nav>
);
}
}
this a react app which has other component other then nav bar but the react routing is not working, Can any one help on this?
Also i have used some basic bootstrap code just for testing.
the result are here where im clicking one thing and it coming up with 3 statements from other 3 pages in this same page.
enter image description here
In app.js, import Switch from react-router-dom and wrap your routes in Switch:
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
...
<Switch>
<Route path="/" component={ExercisesList} />
<Route path="/edit/:id" component={EditExercise} />
<Route path="/create" component={CreateExercise} />
<Route path="/create" component={CreateUser} />
</Switch>

React - Url is updating but component is not rerendering

I'm using react-router-dom for routing and the problem is that url is changing when clicked on navigation bar elements, but components belonging to other pages is not rerendering. Here are my codes:
App.js:
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import Header from './common/Header';
import Footer from './common/Footer';
import MainPage from './layout/MainPage';
import GiftWrapping from './layout/GiftWrapping';
import Contact from './layout/Contact';
import Catalog from './layout/Catalog';
function App() {
return (
<div className="App">
<Router>
<Header />
<Switch>
<Route path="/home" component={MainPage} />
<Route path="/catalog" component={Catalog} />
<Route path="/giftwrapping" component={GiftWrapping} />
<Route path="/contact" component={Contact} />
</Switch>
<Footer />
</Router>
</div>
);
}
export default App;
Header.js:
<nav className="Header-bottom">
<div className="container clearfix">
<Router>
<div className="Left-nav">
<ul>
<li>
<Link to={"/home"} className="active-nav-element">Main Page</Link>
</li>
<li>
<Link to={"/catalog"}>Catalog</Link>
</li>
<li>
<Link to={"/contact"}>Contact</Link>
</li>
<li>
<Link to={"/giftwrapping"}>Gift Wrapping</Link>
</li>
<li>
<Link to={"/catalog"} className="dropdown-toggle">Catalog</Link>
</li>
</ul>
</div>
</Router>
</div>
</nav>
import React from "react";
import { Link, withRouter } from "react-router-dom";
function Header() {
return (
<nav className="Header-bottom">
<div className="container clearfix">
<div className="Left-nav">
<ul>
<li>
<Link to={"/home"} className="active-nav-element">
Main Page
</Link>
</li>
<li>
<Link to={"/catalog"}>Catalog</Link>
</li>
<li>
<Link to={"/contact"}>Contact</Link>
</li>
</ul>
</div>
</div>
</nav>
);
}
export default withRouter(Header);
Instead of adding a separate <Router> in the <header /> please use the withrouter method.
Codesandbox reference

How to use react router

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

Some Links not working with JSX

I am building a web app to learn react and when I tried to make the menu, my menu links doesn't work. The strange thing is that I am using materialize css and the mobile menu works perfectly but in large screens, as my computer, it doesn't work and the Links tags are the same! Help!
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import Features from './pages/Features';
import Archives from './pages/Archives';
import Settings from './pages/Settings';
import Home from './pages/Home';
class Index extends React.Component{
print(){
console.log("TESTE####");
}
render(){
return(
<div>
<Router>
<div>
<nav className="light-blue lighten-1" role="navigation">
<div id="navbar" className="nav-wrapper container">
<Link to="/" className="brand-logo">App Teste</Link>
<div className="right hide-on-med-and-down">
<ul className="tabs tabs-transparent" style={{height: 64}}>
<li className="tab" style={{paddingTop: 10}}><Link to="features" onClick={this.print}>Features</Link></li>
<li className="tab" style={{paddingTop: 10}}><Link to="archives">Archives</Link></li>
<li className="tab" style={{paddingTop: 10}}><Link to="settings">Settings</Link></li>
</ul>
</div>
<ul id="nav-mobile" className="side-nav">
<li><Link to="features">Features</Link></li>
<li><Link to="archives">Archives</Link></li>
<li><Link to="settings">Settings</Link></li>
</ul>
<i className="material-icons">menu</i>
</div>
</nav>
<Route exact path="/" component={Home}/>
<Route path="/archives" component={Archives}/>
<Route path="/settings" component={Settings}/>
<Route path="/features" component={Features}/>
</div>
</Router>
</div>
);
}
}
ReactDOM.render(
<Index/>,
document.getElementById('root')
);
On each of my pages I just have a <h1></h1> saying what is the page at the moment. So if it is the Home page it shows Home, if it is the Archives page it shows Archives..
One last thing, when I click at any link, nothing happens, doesn't load the pages and doesn't change the url.

Resources