Route does not render components - reactjs

There is a problem using routes in KnowledgeBase.js (code below). I'm trying to write them "inside" another Route (which is in App.js), but nothing works. Perhaps someone can find the problem.
p.s. the console does not give any errors
App.js
import React from "react";
import './App.css';
import Header from "./Components/Header/Header";
import Main from "./Components/Main/Main";
import {Route, Routes} from "react-router-dom";
import Home from "./Components/Main/Home/Home";
import Nav from "./Components/Nav/Nav";
import KnowledgeBase from "./Components/Main/KnowlegeBase/KnowledgeBase";
import Messages from "./Components/Main/Messages/Messages";
function App(props) {
return (
<div className="App">
<Header forHeader={props.state}/>
<Nav forNav={props.state}/>
<Main forMain={props.state}>
<Routes>
<Route path='/home' element={<Home forHome={props.state}/>}/>
<Route path='social-network/' element={<Home forHome={props.state}/>}/>
<Route path='/knowledge-base/*' element={<KnowledgeBase forKnowledgeBase={props.state}/>}/>
<Route path={"/messages"} element={<Messages forMessages={props.state}/>}/>
</Routes>
</Main>
</div>
);
}
export default App;
KnowledgeBase.js
import React from "react";
import style from './KnowledgeBase.module.css';
import Menu from "./Menu/Menu";
import Blocks3 from "./Blocks3/Blocks3";
import {Route, Routes} from "react-router-dom";
import BaseAll from "./BaseAll/BaseAll";
import Blocks2 from "./Blocks2/Blocks2";
import Blocks1 from "./Blocks1/Blocks1";
const KnowledgeBase = (props) => {
return (
<div className={style.knowledgeBase}>
<Menu forMenu={props.forKnowledgeBase}/>
<BaseAll forBaseAll={props.forKnowledgeBase}>
<Routes>
<Route path="/knowledge-base/purchase-and-refund" element={<Blocks3/>}/>
<Route path="/knowledge-base/popular-questions" element={<Blocks2/>}/>
<Route path="/knowledge-base/analytics" element={<Blocks1/>}/>
</Routes>
</BaseAll>
</div>
)
}
export default KnowledgeBase
BaseAll.js
import React from "react";
import style from './BaseAll.module.css'
const BaseAll = (props) => {
return (
<div className={style.baseAll}>
{props.children}
</div>
)
}
export default BaseAll
Menu.js (it has all NavLink)
import React from "react";
import style from './Menu.module.css'
import {NavLink} from "react-router-dom";
const Menu = (props) => {
return (
<div className={style.menu}>
<header>{props.forMenu.menu.header}</header>
<form action="">
<div>
<img src={props.forMenu.menu.search_img} alt=""/>
<input type="text" placeholder={props.forMenu.menu.input}/>
</div>
<button type={"submit"}>{props.forMenu.menu.search_btn}</button>
</form>
<nav>
<NavLink to={"/knowledge-base/analytics"} className={style.menu_NavLink}>{props.forMenu.menu.nav1}</NavLink>
<NavLink to={"/knowledge-base/popular-questions"} className={style.menu_NavLink}>{props.forMenu.menu.nav2}</NavLink>
<NavLink to={"/knowledge-base/purchase-and-refund"} className={style.menu_NavLink}>{props.forMenu.menu.nav3}</NavLink>
</nav>
</div>
)
}
export default Menu
I expect the relevant blocks to appear under the search menu when navigating through NavLink. I tried to implement many options in App.js and KnowledgeBase.js but couldn't find one that would work

In KnowledgeBase.js file, remove /knowledge-base from each route.
React router automatically adds the parent route path with each child. You do not have to write the parent path with each child if it's already placed inside with another route i.e. /knowledge-base/*
const KnowledgeBase = (props) => {
return (
<div className={style.knowledgeBase}>
<Menu forMenu={props.forKnowledgeBase}/>
<BaseAll forBaseAll={props.forKnowledgeBase}>
<Routes>
<Route path="/purchase-and-refund" element={<Blocks3/>}/>
<Route path="/popular-questions" element={<Blocks2/>}/>
<Route path="/analytics" element={<Blocks1/>}/>
</Routes>
</BaseAll>
</div>
)
}

Related

A <Route> is only ever to be used as the child of <Routes> element, never rendered directly

I was trying to display my layout.jsx on the browser but keep getting "A <Route> is only ever to be used as the child of Routes element, never rendered directly;" error I tried wrapping it in Routes but keep getting the error.
import React from 'react'
import Sidebar from '../sidebar/Sidebar';
import Routes from '../Routes';
import {BrowserRouter, Route} from 'react-router-dom';
const Layout = () => {
return (
<BrowserRouter>
<Route render={(props) =>(
<div className='layout'>
<Sidebar {...props} />
<div className="layout__content">
<div className="layout__content-main">
<Routes />
</div>
</div>
</div>
)
} />
</BrowserRouter>
)
}
export default Layout
import React from 'react';
import {Route, Routes} from 'react-router-dom';
import Dashboard from '../pages/Dashboard';
import Customers from '../pages/Customers';
const RRoutes = () => {
return (
<Routes>
<Route path='/' component={Dashboard} />
<Route path='/customers' component={Customers}/>
</Routes>
)
}
export default RRoutes
import React from 'react'
const Sidebar = () => {
return (
<div>
Hello Sidebar
</div>
)
}
export default Sidebar
Since you are using react-router-dom#6 the Route component API changed quite a bit. There are no longer any component or render and children function props, they were replaced by a single element prop taking a ReactNode, a.k.a. JSX. Additionally, all Route components must be rendered by a Routes component, the spiritual successor to the v5 Switch component that handles route matching and rendering.
import React from 'react'
import {BrowserRouter, Route} from 'react-router-dom';
import Sidebar from '../sidebar/Sidebar';
import MyRoutes from '../Routes';
const Layout = () => {
return (
<BrowserRouter>
<div className='layout'>
<Sidebar />
<div className="layout__content">
<div className="layout__content-main">
<MyRoutes />
</div>
</div>
</div>
</BrowserRouter>
);
}
...
import React from 'react';
import { Route, Routes } from 'react-router-dom';
import Dashboard from '../pages/Dashboard';
import Customers from '../pages/Customers';
const MyRoutes = () => {
return (
<Routes>
<Route path='/' element={<Dashboard />} />
<Route path='/customers' element={<Customers />}/>
</Routes>
);
}
export default MyRoutes;

How to have a one whole file as a router file in react

I tried doing this and I had an error:
Uncaught Error: 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:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app.
I'm new to react and what I'm trying to do is making a universal router for all of my components. Is it a good idea to have all routes on a single file or have it on every file that needed it? Here are my code
Index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import createRoutes from './routes';
const Routes = createRoutes();
ReactDOM.render(Routes,document.getElementById('root'))
routes.js:
import {BrowserRouter as Router,Route} from "react-router-dom";
// import {Home, Settings} from './routes/index'
import Home from './routes/Home'
import Settings from './routes/settings'
const createRoutes = () => (
<Router>
<Route exact path="/" component={Home}/>
<Route exact path="/home" component={Home}/>
<Route exact path="/settings" component={Settings}/>
</Router>
);
export default createRoutes
Edit: Here are my Home.js and Settings.Js Files:
Home.js:
import '../styles/home.scss'
import Cards from '../components/swiper'
import Slideshow from '../components/slideshow'
const home = ()=>{
let user = "user"
return(
<>
<Slideshow/>
<h1 className="welcome"> Welcome {user},</h1>
<div className="flex Create-Event">
<h2 className="title-headers">Featured events:</h2>
Create Event
</div>
<Cards/>
<h2 className="title-headers">Upcoming events:</h2>
<Cards/>
<div className="help flex">
<h2>Need assistance? </h2>
Contact Us!
</div>
</>
)
}
export default home
settings.js:
//todo : immport form and other types of settings related components here later on
function Settings() {
return (
<div>
<h1>Settings goes here</h1>
</div>
)
}
export default Settings
Instead of using a hook, use a regular function.
EDIT: Try this. Also, I just noticed you're using home hook, when you're calling Home in Router.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import createRoutes from './routes';
import MyRouter from "./MyRouter";
ReactDOM.render(
<MyRouter />,
document.getElementById('root'))
MyRouter.js
import {BrowserRouter as Router,Route,Routes} from "react-router-dom";
// import {Home, Settings} from './routes/index'
import Home from './routes/Home'
import Settings from './routes/settings'
function MyRouter() (
<Router>
<Routes>
<Route exact path="/" component={<Home />}/>
<Route exact path="/home" component={<Home />}/>
<Route exact path="/settings" component={<Settings />}/>
</Routes>
</Router>
);
export default MyRouter
Home.js
import '../styles/home.scss'
import Cards from '../components/swiper'
import Slideshow from '../components/slideshow'
function Home() {
let user = "user"
return(
<>
<Slideshow/>
<h1 className="welcome"> Welcome {user},</h1>
<div className="flex Create-Event">
<h2 className="title-headers">Featured events:</h2>
Create Event
</div>
<Cards/>
<h2 className="title-headers">Upcoming events:</h2>
<Cards/>
<div className="help flex">
<h2>Need assistance? </h2>
Contact Us!
</div>
</>
)
}
export default Home
settings.js
function Settings() {
return (
<div>
<h1>Settings goes here</h1>
</div>
)
}
export default Settings

Unable to see the text returned from home and tutorials page in react-hooks site

Unable to see the header text returned in my Home and Tutorials page in my react hooks site. Below is my App.js, Navigation.js and Home.js. Could someone please advise about the problem here.
App.js
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import './App.css';
import Home from "./components/Home";
import Tutorials from "./components/Tutorials";
import Navigation from './components/Navigation';
function App() {
return (
<BrowserRouter>
<Navigation>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</Navigation>
</BrowserRouter>
);
};
export default App;
Navigation.js
import React from 'react';
import { NavLink} from 'react-router-dom';
const Navigation = () => {
return (
<div className="App">
<div className="wrapper">
<div id="wrap">
<nav className="siteNavigation_nav_links">
<div className="row" ></div>
<div className="main_links_nav">
<NavLink className="mob_link" to="/">Home</NavLink>
<NavLink className="mob_link" to="/tutorials">Tutorials</NavLink>
</div>
</nav>
</div>
</div>
</div>
)
}
export default Navigation;
Home.js
import React from 'react';
const Home = () =>{
return (
<div className="App">
<h1>This is Home Page !</h1>
</div>
)
}
export default Home;
Tutorials.js
import React from 'react';
const Tutorials = () =>{
return (
<div><h1>This is Tutorials Page !</h1></div>
)
};
export default Tutorials;
Navigation is a Navbar component. And you want it everywhere on the site.
So, you can easily do it by using it outside the scope of the Switch component.
In doing so, it will render all over your screens.
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</BrowserRouter>

Having Trouble with ReactJS Links

I started off thinking this was going to be an easy issue; however I was wrong. I have the following home page:
import React,{Component} from "react";
import {Link} from "react-router-dom";
const Home = (props) =>{
return(
<div>
<h1>My Portal Home</h1>
Click <Link to="/login">Here</Link> To Log In.
</div>
)
}
export default Home;
My main component looks like this: I am certain it is NOT what it is supposed to look like:
import React, {Component} from "react";
import {Router, Route, Link} from "react-router-dom";
import Login from './login/login';
import Home from './home/home';
const Main = (props) =>{
return(
<Router>
<div>
<Route path="/" component={Home}/>
<Route path="/login" component={Login}/>
</div>
</Router>
);
};
export default Main;
Finally my index:
import React from 'react';
import ReactDOM from 'react-dom'
import Main from "./main";
import './index.css';
ReactDOM.render(<Main />,document.getElementById('root'));
When I enter the site address, the home pages does not show and I get errors (warnings):
Failed prop type: Invalid prop 'component' supplied to 'Route': the prop is not a valid React component
in Route (at main.js:11)
I am OBVIOUSLY doing this wrong, but my goal is to have a home page with a link to my login page. Assistance is greatly appreciated.
Try the following:
Main.js
import React from "react";
import {BrowserRouter, Route} from "react-router-dom";
import Login from './Login';
import Home from './Home';
const Main = () =>{
return(
<BrowserRouter>
<div>
<Route exact path="/" component={Home}/>
<Route path="/login" component={Login}/>
</div>
</BrowserRouter>
);
};
export default Main;
Home.js
import React from "react";
import {Link} from "react-router-dom";
const Home = () =>{
return(
<div>
<h1>My Portal Home</h1>
Click <Link to="/login">Here</Link> To Log In.
</div>
)
};
export default Home;
Login.js
import React from "react";
const Login = () =>{
return(
<div>
Login
</div>
);
};
export default Login;
Made a CodeSandbox, hopefully this helps
CodeSandbox
Main.js
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const Main = () => (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
<hr />
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
</div>
</Router>
);
const Home = () => (
<div>
<h2> Home </h2>
</div>
);
const Login = () => (
<div>
<h2> Login Page </h2>
</div>
);
export default Main;

ReactJS - I can't reach a component inside in other by route

I have an website with this structure:
<Header>
<MenuPrincipal>
<Home></Home>
</MenuPrincipal>
</Header>
When I click on a logo in the Header, it isn't working, nothing is happening and I would like to access the Home component The <Home> component is inside of <MenuPrincipal>. The <Header> component is the parent component, how you can see in the structure above. How can I solve it?
import React from 'react'
import { BrowserRouter as Router, Route, Link, } from 'react-router-dom'
import Home from './Home';
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import { showBGImage } from './actions';
const Header = (props) => {
return (
<div className="header">
<div>
<Router>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Router>
</div>
</div>
)
}
export default Header
<BrowserRouter> it's more like a history browser, you need to use Switch instead.
import { BrowserRouter as Router, Route, Link, Switch} from 'react-router-dom'
const Header = (props) => {
return (
<div className="header">
<Router>
<Switch>
<div>
<h1>
<Link onClick={showBGImage} className="home-link" to='/'>Mauro Bitar<br />
<small className="home-link">arquiteto</small>
</Link>
</h1>
<Route exact path='/' render={() => <Home />} />
</div>
</Switch>
</Router>
</div>
)
}

Resources