basic components not rendering - reactjs

I'm just starting my first react web app but my components are not rendering. even a simple h1 element directly in the app.js file is not rendering. it did, however, after deleting the Routes, so my best guess is that the problem lies somewhere there.
the code:
app.js
import React, { lazy, Suspense } from 'react'
import './App.css';
import { Routes, Route } from 'react-router-dom'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
const Home = React.lazy(() => import('./pages/Home'))
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<h1>App</h1>
<ToastContainer />
<Routes>
<Route exact path="/" element={Home} />
</Routes>
</Suspense>
)
}
export default App;
Home.js
import React from 'react'
const Home = () => {
return (
<h1>Home Page</h1>
)
}
export default Home
sorry for this very beginner question. please keep in mind that this is my first react web app.

React route accepts ReactElement not ReactNode, no need of exact also
<Routes>
<Route path="/" element={<Home />} />
</Routes>

I think that the tutorial you are using uses the previous react-router API. Please, try this:
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useRouteMatch,
useParams
} from "react-router-dom";
export default function App() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
function Home() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}

Related

React dom route

Here is my App.js code
import './App.css';
import Home from './Pages/Home';
import About from './Pages/About';
import Contact from './Pages/Contact';
import {BrowserRouter,Routes,Route ,Link} from 'react-router-dom';
function App() {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home/>}/>
<Route Path="/About" element={<About/>}/>
<Route Path="/Contact" element={<Contact/>}/>
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
and Here is my About page
import React from 'react';
import {Link} from 'react-router-dom';
function About() {
return (
<div>
<h1>This is About</h1>
<Link to="About">Go to About</Link>
</div>
)
}
export default About
React Route is not working.I installed react router Dom already.please help me to fix
Not navigation working
Can you change this
<Link to="About">Go to About</Link>
to
<Link to="/About">Go to About</Link>
I have added / before the "About" in to.

functional component disappears while using react-router-dom

I just wanted to make a simple project setup with react-router-dom but whenever I'm using route the entire page becomes blank. my Nav disappears. why ?
there was a similar question for class component so it wasn't helpful for me.
App.js :
import "./App.css";
import Nav from "./components/Nav";
import Products from "./components/Products";
import About from "./components/About";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
function App() {
return (
<>
<Router>
<Nav />
<Route path="/about" component={About} />
<Route path="/products" component={Products} />
<About />
</Router>
</>
);
}
export default App;
Nav:
import React from "react";
import Navstyle from "../styles/Nav.module.css";
const Nav = () => {
return (
<nav className={Navstyle.Nav}>
<ul className={Navstyle.nav_links}>
<li>
Home
</li>
<li>
Products
</li>
<li>
About
</li>
</ul>
</nav>
);
};
export default Nav;
other components are just returning h2 tags
You need to use a layout (as a HOC) to add a navbar or other things to your code.
just use the components with routes in your router.
I recommend defining the Layout in another file.
export default function App() {
return (
<Layout>
<Router>
<Route component={Products} path="/products" exact />
<Route component={About} path="/about" exact />
</Router>
</Layout>
);
}
const Products = () => {
return <p>Products</p>;
};
const About = () => {
return <p>about</p>;
};
const Navbar = () => {
return <p>navbar</p>;
};
const Layout = ({ children }) => {
return (
<div>
<Navbar />
<div>{children}</div>
</div>
);
};
I find out ! the problem was YouTube videos I guess. first of all you must add BrowserRouter to your index.js not app.js , like this :
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
after that you must use react router in that way , not the way I tried first :
import "./App.css";
import Nav from "./components/Nav";
import Products from "./components/Products";
import About from "./components/About";
import { Route, Routes } from "react-router-dom";
function App() {
return (
<>
<Nav />
<Routes>
<Route path="/About" element={<About />} />
</Routes>
</>
);
}
export default App;
during the changes of react-router-dom in version 6 , this is the new way of using router.
the link of docs :
https://reactrouter.com/docs/en/v6/getting-started/overview

Error: useHref() may be used only in the context of a <Router> component

**When i Write my My Nav Bar Component Content Directly in My Router Component. It works Fine But When I write that content in a NavBar Component it Generates the following error
Error: useHref() may be used only in the context of a component.
.**
Im Using
"react-dom": "^17.0.2", "react-router-dom": "^6.0.0-beta.6",
Following are my Components..
My NavBar Component:-
import { Link } from "react-router-dom";
// import styles from './NavBar.module.css';
export const NavBar = () => {
return (
<nav>
<Link to="/"> Home </Link>
<Link to="/about"> About </Link>
<Link to="/product"> Product </Link>
</nav>
);
}
My Route Component:-
import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";
// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";
export const RouterConfig = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};
import {BrowserRouter as Router} from "react-router-dom";
Use in components App.js or above in index.js and wrap your component in Router.
Then the problem will disappear ;-)
In Your case I see two option:
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
OR use App Component instead of RouterConfig Component.
App.js
import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";
// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";
export const App = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};
I tried to explain it as best I could. Hope this helps you ;-) Best Greetings and Good Luck!
If you're still confused, check out here =>
React Router V6 - Error: useRoutes() may be used only in the context of a <Router> component
export is not running properly and causing an error. Can you retype it like below and try?:
import { Link } from "react-router-dom";
const NavBar = () => {
return (
<div>
<Link to="/"> Home </Link>
<Link to="/about"> About </Link>
<Link to="/product"> Product </Link>
</div>
);
}
export default Navbar;
Also, you don't need curly brace for import statement.
import NavBar from "./Components/NavBar/NavBar";
If problem still persists, you can check your file paths because you are repeating folder names twice, it looks suspicious to me. Maybe you mean this:
import NavBar from "./Components/NavBar";

NavLink does not add activeClassName (react-router-dom v5.2.0)

I have a small problem with Nav Link.
It doesn't apply active Class Name, although it otherwise works (goes to the desired path, renders the required component)
react-router-dom version 5.2.0
my index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './components/App';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
and my Navigation.js:
import { NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav className="navigation">
<ul className="navigation__list">
<li>
<NavLink to="/" activeClassName="navigation__item_active" className="navigation__item">
Home
</NavLink>
</li>
<li>
<NavLink to="/frontend" className="navigation__item" activeClassName="navigation__item_active">
frontend
</NavLink>
</li>
<li>
<NavLink to="/about" className="navigation__item" activeClassName="navigation__item_active">
about
</NavLink>
</li>
</ul>
</nav>
);
}
export default Navigation;
UPD:
Router works, trouble in NaVLink.
I need to add activeClassName to nav item, but it doesnt.
my App.js:
import React from 'react';
import Header from './Header';
import Footer from './Footer';
import Main from './Main';
function App() {
return (
<div className="App">
<Header/>
<Main/>
<Footer/>
</div>
);
}
export default App;
and Main.js:
import React from 'react';
import { Route } from 'react-router-dom';
import HelloPage from './Hello-page';
import About from './About';
import Frontend from './Frontend';
import Navigation from './Navigation';
function Main() {
return (
<main className="main">
<Route exact path="/">
<HelloPage/>
</Route>
<Route path="/about">
<About/>
</Route>
<Route path="/frontend">
<Frontend/>
</Route>
<Navigation/>
</main>
);
}
export default Main;
it fixed by 1 command... (10+ hours of my live)
npm i path-to-regexp#1.7.0 -S
It should already work tho~
Btw, even if you applied those activeClassName. You still need to tweak your code in App.js to something like this:-
need to add Redirect only for the home or / route. If not, the activeClassName will be applied always for home or / route
App.js:-
export default function App() {
return (
<Router>
<Nav />
<Switch>
{/* Add this Redirect */}
<Redirect exact from="/" to="/home" />
<Route path="/home" component={() => "Home page"} />
<Route path="/demo" component={() => "Demo page"} />
<Route path="/demo2" component={() => "Demo2 page"} />
</Switch>
</Router>
);
}
You can see this working sandbox
Try to delete the Redirect and see how it changes
As mentioned here in my case solution was replace in webpack
resolve: {
modules: [
path.resolve('./src'),
- path.resolve('./node_modules'),
+ 'node_modules',
],
},

Lazy route component doesn't work if rendered by another component

I've built a toggle component that creates the code chunks for a list of feature components based on the env variable set for the build.
e.g.:
import React from "react";
import OtherComponent from "./OtherComponent";
import { ToggleFeature, features } from "./ToggleFeature";
export default function App() {
return (
<>
<OtherComponent />
<ToggleFeature feature={features.CustomRoute} suspense={false} />
</>
);
}
This approach works for almost every component, except for Route, it seems to break any routes bellow.
I've thought that the Suspense in the middle of the Switch was breaking the code, so I've created a boolen prop called suspense to deactivate it inside the ToggleFeature, leaving this responsability to the code calling it.
This works:
import React, { Suspense } from "react";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
import Home from "./Home";
import RouteCommon from "./RouteCommon";
import { features } from "./ToggleFeature";
export default function App() {
return (
<>
<BrowserRouter>
<Link to="/">Home</Link> •
<Link to="/common">Common Route</Link> •
<Link to="/lazy">Lazy Route</Link>
<Suspense fallback={null}>
<Switch>
<Route path="/common" component={RouteCommon} />
<Route path="/lazy" component={features.CustomRoute} />
<Route path="/" component={Home} />
</Switch>
</Suspense>
</BrowserRouter>
</>
);
}
This doesn't work:
import React, { Suspense } from "react";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
import Home from "./Home";
import RouteCommon from "./RouteCommon";
import { ToggleFeature, features } from "./ToggleFeature";
export default function App() {
return (
<>
<BrowserRouter>
<Link to="/">Home</Link> •
<Link to="/common">Common Route</Link> •
<Link to="/lazy">Lazy Route</Link>
<Suspense fallback={null}>
<Switch>
<Route path="/common" component={RouteCommon} />
<ToggleFeature feature={features.CustomRoute} suspense={false} />
<Route path="/" component={Home} />
</Switch>
</Suspense>
</BrowserRouter>
</>
);
}
Is this approach possible for this situation?
Here's a fiddle with the above code running: https://codesandbox.io/s/toggle-route-kvqp5

Resources