Hello cannot under stand how its work with this import and routes i created a navbar , created couples of pages like videolectures , so i create a router (link) to the videolectures page but then i press from main page there is a navbar displayed on VideoLecture i get white screen and its nothing going on display NavBar is gone i also try to write some code inside a VideoLecture page but its still not going to dislpay anyway CodeBelow
NavBar.js
import React from 'react';
import {Navbar,Nav,Container} from 'react-bootstrap';
import {Routes, Route, Link} from "react-router-dom";
import logo from '../img/logo1.jpg';
import Videolectures from '../pages/videolectures'
export default function NavBar() {
return(
<div>
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
<Container>
<img
src= { logo }
width="35"
height="35"
className="d-inline-block align-top rounded-circle"
alt = ""
href= "#home"
/>
<Navbar.Brand>React-Bootstrap</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="me-auto">
<Nav.Link as={Link} to="/videolectures">VideoLectures</Nav.Link>
<Nav.Link href="#pricing">Questions</Nav.Link>
</Nav>
<Nav>
<Nav.Link href="#deets">Login</Nav.Link>
<Nav.Link eventKey={2} href="#memes">
Sing Up
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
<Routes>
<Route path="/videolectures" element={<Videolectures/>}/>
</Routes>
</div>
);
}
app.js
import NavBar from './components/navbar';
import React from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
function App() {
return (
<NavBar/>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'bootstrap/dist/css/bootstrap.min.css';
import {BrowserRouter,Route,Routes} from 'react-router-dom';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={ <App/> }>
</Route>
</Routes>
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
VideoLecture.js
import React from 'react';
import {Card,Button} from 'react-bootstrap';
export default function Videolectures() {
return(
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
);
}
I also get messsage from web console
index.tsx:25 You rendered descendant <Routes> (or called `useRoutes()`) at "/" (under <Route path="/">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render.
Please change the parent <Route path="/"> to <Route path="*">.
warning # index.tsx:25
index.tsx:25 No routes matched location "/videolectures"
Try to move the BrowserRouter initialization in the App component and add to it also the /videolectures Route.
Also change the / route element to NavBar:
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<NavBar />}></Route>
<Route path='/videolectures' element={<Videolectures />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Of course, you will need to change the index.js back to
ReactDOM.render(
<React.StrictMode>
<App/>
</React.StrictMode>,
document.getElementById('root')
);
Related
I have below react-js code, which written for showing the navigation bar
When I click the links, the browser address changes BUT redirection is not happening
import React from "react";
import Navbar from "react-bootstrap/Navbar";
import { Nav } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import {
HomeRounded,
SchoolRounded,
WorkRounded,
Facebook,
Twitter,
LinkedIn,
GitHub,
Telegram,
} from "#mui/icons-material";
import { Link, NavLink, withRouter } from "react-router-dom";
import resumeData from "../../utils/resumeData";
import CustomButton from "../Button/Button";
import "./Header.css";
const Header = (props) => {
const pathName = props?.location?.pathname;
console.log(pathName);
return (
<Navbar expand="lg" sticky="top" className="header">
<Nav.Link as={NavLink} to="/">
<Navbar.Brand className="header_home">
<HomeRounded />
</Navbar.Brand>
</Nav.Link>
<Navbar.Toggle />
<Navbar.Collapse>
<Nav>
<Nav.Link
as={NavLink}
to="/"
className={pathName == "/" ? "header_link_active" : "header_link"}>
Resume
</Nav.Link>
<Nav.Link
as={NavLink}
to="/portfolio"
className={
pathName == "/portfolio" ? "header_link_active" : "header_link"}>
Portfolio
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
};
export default withRouter(Header);
Here is my App.js
import React from "react";
import { BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import {Container, Grid } from "#material-ui/core";
import Profile from "./components/Profile/Profile";
import Header from "./components/Header/Header";
import Portfolio from "./pages/Portfolio/Portfolio";
import Resume from "./pages/Resume/Resume";
import Footer from "./components/Footer/Footer";
function App() {
return (
<Container className={'top_60'}>
<Grid container spacing={7}>
<Grid item xs={12} sm={12} md={4} lg={3}>
<Profile/>
</Grid>
<Grid item xs>
<Router>
<Header/>
<Switch>
<Route path="/portfolio">
<Portfolio />
</Route>
<Route path="/">
<Resume />
</Route>
</Switch>
</Router>
<Footer/>
</Grid>
</Grid>
</Container>
);
}
export default App;
It works fine ONLY when I manually click ENTER on browser URL!
here is the error in console
on app.js you have to declare the routes on this way:
<Route path="/" element={<Home />} />
I'm a beginner front-end dev and I try to make an imaginary website. I'm learning to react router now. So when I try to click on the home/about or contact button, It doesn't open it. What am I doing wrong?
here's my code below :
my App code :
import React from 'react';
import './App.css';
import Navibar from './Components/Navibar';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Home from './Components/Home';
import About from './Components/About';
import Contact from './Components/Contact';
let App = () => {
return (
<Router>
<div className="App">
<Navibar/>
<div className="content">
<Switch>
<Route exact path="/">
<Home/>
</Route>
<Route exact path="/About">
<About/>
</Route>
<Route exact path="Contact">
<Contact/>
</Route>
</Switch>
</div>
</div>
</Router>
);
}
export default App;
and here's my navbar code :
import React from 'react';
import {Nav, Navbar, Container} from 'react-bootstrap';
import {Link} from 'react-router-dom';
let Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand href="#home">My Portfolio</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link>
<Link to="#about">About me</Link>
<Link to="#contact">Contact</Link>
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
and here's my Home (the home/about and contact codes are the same) code :
import React from 'react';
let Home = () => {
return (
<>
<h2>Home</h2>
</>
);
}
export default Home;
I need help from a professional, because I'm getting stuck in this :)
The Navibar should link to the route paths you are rendering, not anchor tags (hashes) on the current page. The bootstrap Navbar.Brand component should also render a RRD Link component instead of a plain anchor tag with href attribute.
import React from 'react';
import { Nav, Navbar, Container} from 'react-bootstrap';
import { Link } from 'react-router-dom';
const Navibar = () => {
return (
<>
<Navbar bg="dark" variant="dark">
<Container>
<Navbar.Brand
as={Link} // <-- as a RRD Link component
to="/" // <-- to prop for Link component
>
My Portfolio
</Navbar.Brand>
<Nav className="me-auto">
<Link to="/">Home</Link> // <-- "/"
<Link to="/about">About me</Link> // <-- "/about"
<Link to="/contact">Contact</Link> // <-- "/contact"
</Nav>
</Container>
</Navbar>
</>
);
}
export default Navibar;
The App component should specify route paths that match what the navbar is linking to.
Also the routes App is rendering within the Switch component should be rendered in inverse order of route path specificity. In other words, render the more specific paths prior to the less specific paths. This is to attempt to match and render more specific paths and falling back to less specific paths. This also effectively eliminates the need to use the exact prop.
const App = () => {
return (
<Router>
<div className="App">
<Navibar />
<div className="content">
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/contact">
<Contact />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</div>
</Router>
);
}
I am learning reactjs and creating a test quiz project with it. The plan is only logged-in users will be able to access a quiz.
I am using react-router. When I have already accessed the website and then accessing different pages, it's not causing any problem. but when I am trying to access a route directly, the component of that route is rendering twice and then getting redirected to the home page. To clarify the situation with an example, I have a route /quiz. when I visit http://localhost:3000/quiz directly in the browser, it prints the following message in the console.
[HMR] Waiting for update signal from WDS...
Quiz.js:10 Quiz rendered
Quiz.js:10 Quiz rendered
app.js:5 works
then I am being redirected to http://localhost:3000/.
So, my problems are:
I am getting rendered twice
I am being redirected.
I have attached the route and quiz component here. What is causing the issue?
Routes.js
import React, {useState} from 'react'
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import Home from '../public_page_components/Home'
import About from '../public_page_components/About'
import SearchPage from '../product_search_components/SearchPage'
import NoMatch from '../public_page_components/NoMatch'
import MyNav from './MyNav'
import Todo from '../public_page_components/Todo'
import Quiz from '../public_page_components/Quiz'
import Footer from '../public_page_components/Footer'
export default function Routes(props) {
var isSigned = localStorage.getItem('scs_user_logged_in') ? true : false
const [signedIn, setSignedIn] = useState(isSigned)
return (
<Router>
<MyNav signedIn={signedIn} setSignedIn={setSignedIn} search_item={props.search_item} setSearchItem={props.setSearchItem}/>
<Switch>
<Route path='/' exact>
<Home search_item={props.search_item} setSearchItem={props.setSearchItem} />
</Route>
<Route path='/about' exact component={About} />
<Route path='/search/:search_term' children={<SearchPage search_item={props.search_item} setSearchItem={props.setSearchItem}/>} />
<Route path="/todo" exact>
<Todo/>
</Route>
<Route path="/quiz" exact>
<Quiz signedIn={signedIn} setSignedIn={setSignedIn}/>
</Route>
<Route path="*" component={NoMatch} />
</Switch>
<Footer/>
</Router>
)
}
Quiz.js
import React from 'react'
import Button from 'react-bootstrap/Button'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
export default function Quiz(props) {
console.log('Quiz rendered')
let quiz_menu = props.signedIn ? <div style={{textAlign:"right"}}>Logged in</div> : "not logged in"
return (
<>
<div style={{minHeight:"80vh"}}>
<Row>
<Col>
<h1>
Quiz
</h1>
</Col>
<Col style={{display:"flex", alignItems:"center"}}>
{quiz_menu}
</Col>
</Row>
<Row style={{margin:"5px 0px"}}>
<Col xs={9} style={{backgroundColor:"blue"}}>
test
</Col>
<Col xs={3} style={{backgroundColor:"red"}}>
test
</Col>
</Row>
</div>
</>
)
}
MyNav.js
import React from 'react'
import Nav from 'react-bootstrap/Nav'
import Navbar from 'react-bootstrap/Navbar'
// import NavDropdown from 'react-bootstrap/NavDropdown'
import { Link } from 'react-router-dom'
import Searchfield from '../product_search_components/Searchfield'
import LoginSignout from '../login_components/LoginSignout'
import MenuButtons from './MenuButtons'
export default function MyNav(props) {
var menu = props.signedIn ? <LoginSignout setSignedIn={props.setSignedIn}/> : <MenuButtons setSignedIn={props.setSignedIn}/>
return (
<Navbar bg="dark" variant="dark" expand="lg">
<Navbar.Brand><span style={{ fontWeight: 'bold' }}>Test</span></Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Link className='nav-link' to="/">Home</Link>
<Link className='nav-link' to="/about">About</Link>
<Link className='nav-link' to="/todo">Todo</Link>
<Link className='nav-link' to="/quiz">Quiz</Link>
{/* <NavDropdown title="Dropdown" id="basic-nav-dropdown">
<NavDropdown.Item>Action</NavDropdown.Item>
<NavDropdown.Item>Another action</NavDropdown.Item>
<NavDropdown.Item>Something</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item>Separated link</NavDropdown.Item>
</NavDropdown> */}
</Nav>
</Navbar.Collapse>
<Searchfield search_item={props.search_item} setSearchItem={props.setSearchItem} />
{menu}
</Navbar>
)
}
I have looked at various answers related to the subject, but the issue seem to persist. Would be happy if some concrete solution is available.
I am trying to render a navbar using react-bootstrap and trying to route it using react-router. It does not seem to work on the first click. When the page is forced the components get loaded.
Here are the relevant files.
MyAppNavbar,js
import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Nav, NavDropdown, Navbar, Container, Alert } from "react-bootstrap";
import { Link } from 'react-router-dom';
import Routes from './Routes';
class MyAppNavbar extends Component {
render() {
return (
<React.Fragment>
<Navbar collapseOnSelect expand="lg" bg="dark" fixed="top" variant="dark" >
<Navbar.Brand as={Link} to="/" >React-Bootstrap</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav activeKey={window.location.pathname} variant="pills">
<Nav.Item href="/">
<Nav.Link as={Link} to="/" eventKey="/home" title="Home">
Home
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link as={Link} to="/about" title="About">
About
</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link as={Link} to="/category" eventKey="category" title="Category">
Category
</Nav.Link>
</Nav.Item>
<NavDropdown title="Products" id="nav-dropdown">
<NavDropdown.Item>
Basic Pricing
</NavDropdown.Item>
<NavDropdown.Item>
Corporate
</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item> Enterprise pricing
</NavDropdown.Item>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
<Routes />
</React.Fragment>
);
}
}
export default MyAppNavbar;
Routes.js
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import Category from './Category';
import Products from './Products';
import Home from "./Home";
import About from "./About";
import NotFound from './NotFound'
const Routes = () =>
<Router>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/about'>
<About />
</Route>
<Route path='/category'>
<Category />
</Route>
<Route path='/products'>
<Products />
</Route>
<Route >
<NotFound />
</Route>
</Switch>
</Router>
export default Routes;
App.js
import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import Routes from './website/Routes';
import MyAppNavbar from "./website/MyAppNavbar";
const App = () =>
<React.Fragment>
<MyAppNavbar />
<Routes/>
</React.Fragment>
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App/>
</BrowserRouter>,
document.getElementById('root')
);
Try these fixes:-
Since you use NavLink inside MyAppNavBar.js, then it will need BrowserRouter from react-router-dom to be functional.
Then you can either do:-
Fixes 1 - Wrap everything in App.js with BrowserRouter or,
Fixes 2 - You can just remove BrowserRouter in Routes.js
You just need ONE BrowserRouter anyway
Fixes 1
Remove BrowserRouter from both index.js & Routes.js
add & wrap everything inside App.js with BrowserRouter
'index.js':-
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';
ReactDOM.render(<App/>, document.getElementById('root')
);
Routes.js:-
import React from 'react';
import { Switch, Route, Link } from 'react-router-dom';
import Category from './Category';
import Products from './Products';
import Home from "./Home";
import About from "./About";
import NotFound from './NotFound'
const Routes = () =>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/about'>
<About />
</Route>
<Route path='/category'>
<Category />
</Route>
<Route path='/products'>
<Products />
</Route>
<Route >
<NotFound />
</Route>
</Switch>
export default Routes;
App.js:-
import React, { Component } from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import Routes from './website/Routes';
import MyAppNavbar from "./website/MyAppNavbar";
const App = () =>
<Router>
<MyAppNavbar />
<Routes/>
</Router>
export default App;
Fixes 2
Remove BrowserRouter from Routes.js
Routes.js:-
import React from 'react';
import { Switch, Route, Link } from 'react-router-dom';
import Category from './Category';
import Products from './Products';
import Home from "./Home";
import About from "./About";
import NotFound from './NotFound'
const Routes = () =>
<Switch>
<Route exact path='/'>
<Home />
</Route>
<Route path='/about'>
<About />
</Route>
<Route path='/category'>
<Category />
</Route>
<Route path='/products'>
<Products />
</Route>
<Route >
<NotFound />
</Route>
</Switch>
export default Routes;
I'm fairly new to React and I'm trying to implement Navbar with react-bootstrap. I have the navigation bar set up but its not actually linking to any of my pages.
I've tried looking through the documentation but there's not much information there. I've looked at other posts but they all seem to be using older versions of react-bootstrap or not using it at all.
import React from "react";
import ReactDOM from "react-dom";
import "./components/stylesheets/index.css";
import * as serviceWorker from "./serviceWorker";
import "bootstrap/dist/css/bootstrap.min.css";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));
serviceWorker.unregister();
import React, { Component } from "react";
import "./components/stylesheets/App.css";
import NavBar from "./components/NavBar";
import Home from "./components/Home";
import About from "./components/About";
class App extends Component {
render() {
return <NavBar />;
}
}
export default App;
import React, { Component } from "react";
import { Navbar, Nav, Form, FormControl, Button } from "react-bootstrap";
import "./stylesheets/NavBar.css";
class NavBar extends Component {
state = {};
render() {
return (
<div id="bar">
<Navbar bg="light" variant="light">
<Navbar.Brand href="#home">CALC-U</Navbar.Brand>
<Nav className="ml-auto">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#about">About</Nav.Link>
<Nav.Link href="#updates">Updates</Nav.Link>
<Nav.Link href="#profile">Profile</Nav.Link>
</Nav>
</Navbar>
</div>
);
}
}
export default NavBar;
I want the app to open up to the Home page automatically, and then open up the other pages if the button is clicked on in the navBar.
Edit: I've created a working example, you can check the code: Codesandbox
Seems like you're not using react-router for handling the links.
First you need to wrap your component with a BrowserRouter like so:
import { BrowserRouter } from "react-router-dom";
import App from "./App";
const app = (
<BrowserRouter>
<App />
</BrowserRouter>
);
ReactDOM.render(app, document.getElementById("root"));
And in NavBar.js, inside a <Switch /> component, you declare the routes:
import { Switch, Route, Link } from "react-router-dom";
//(...)
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
In your case, you case use Link component inside your Nav.Link:
<Nav className="ml-auto">
<Nav.Link as={Link} to="/">Home</Nav.Link>
<Nav.Link as={Link} to="/about">About</Nav.Link>
</Nav>
Same applies if you want to add more links to the NavBar.
Sample application using react-bootstrap for Navbar
App.js
function App() {
return (
<div>
<Router>
<NavBar />
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" exact component={About} />
<Route path="/updates" exact component={Updates} />
<Route path="/profile" exact component={Profile} />
</Switch>
</Router>
</div>
);
}
const Home = () => <h1>Home</h1>;
const About = () => <h1>About</h1>;
const Updates = () => <h1>Updates</h1>;
const Profile = () => <h1>Profile</h1>;
NavBar.js
class NavBar extends React.Component {
state = {};
render() {
return (
<div id="bar">
<Navbar bg="light" variant="light">
<Navbar.Brand href="/">CALC-U</Navbar.Brand>
<Nav className="ml-auto">
<Nav.Link href="/">Home</Nav.Link>
<Nav.Link href="/about">About</Nav.Link>
<Nav.Link href="/updates">Updates</Nav.Link>
<Nav.Link href="/profile">Profile</Nav.Link>
</Nav>
</Navbar>
</div>
);
}
}