I'm currently developing a web application with React and react-bootstrap. Everything is runnning fine except the render process of my navigation area on the top of my webpage.
The problem looks as follows:
I want to display a element inside my navigation bar with a couple of elements. But everytime my page is displayed inside the web browser, the element Nav.Link isn't loaded and thus defined as undefined.
Hereis the corresponding file:
import React,{Component} from 'react';
import './HorizontalNavBar.css';
import atom from './res/atom.svg';
import file from './res/file_icon.svg';
import tag from './res/tag.svg';
import user from './res/user_icon.svg';
import {Navbar,Nav,NavDropdown,Form,FormControl} from 'react-bootstrap';
import { bootstrapUtils } from 'react-bootstrap/lib/utils';
class HorizontalNavBar extends React.Component
{
constructor(props)
{
super(props);
this.state =
{
actions:["Files","TagManager","Profile"],
};
}
render()
{
return(
<Navbar bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">
<img
src={atom}
alt="Logo"/>
SciTag
</Navbar.Brand>
<Navbar.Collapse>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default HorizontalNavBar;
Here are a couple of things of solutions I've already tried out:
Checked the official documentation of react-bootstrap
Downloaded the Bootstrap Stylesheet again
Reconfigured webpack
Updated React to the latest stable version
Edit:
I've discovered the source of the error myself. While the latest documentation is explaining how developers can utilize react-bootstrap version 1.0.0-beta.5, I was using my < 1.0.0 version with the latest tutorials. Of course, this didn't work out because the components which are explained it the tutorials are not included in my setup. The solution was to download latest beta version and use the stylesheets from Bootstrap 4 and not Bootstrap 3.
Your issue is in the way you implemented it:
You put the NavLink in the Navbar.Collapse Wrapper. That Navbar.Collapse wrapper is part of ensuring a responsive behaviour of your navigation:
Use the expand prop as well as the Navbar.Toggle and Navbar.Collapse components to control when content collapses behind a button.
Please see here: https://react-bootstrap.github.io/components/navbar/
In fact, your NavLink item is there - but you do not see it because it is hidden unless you press the Navbar.Toggle item. Since you didn't implement the item for now, there is no way of seeing it at all.
Now you have two options: implementing the Navbar component without responsive behaviour or with it.
Option 1, without responsive behaviour (we removed the Navbar.Collapse wrapper):
<Navbar bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">SciTag</Navbar.Brand>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar>
Option 2, with responsive behaviour (we added the Navbar.Collapse wrapper with some options)
<Navbar collapseOnSelect bg="dark" variant="dark" expand="lg">
<Navbar.Brand href="#">SciTag</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse>
<Nav>
<Nav.Link href="#home">Home</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
Working sandbox: https://codesandbox.io/s/8l85rqqk8
Related
Hopefully this is a simple question and answer, and forgive me if I am missing something easy here, as I am still a bit new to coding...
I am working on a new React App and noticed that when I deploy (for testing UI on various devices), my site is rendering fine, yet images and /or video that I add in do not show up. Please note, I have done other React apps where this works perfectly fine, which is where my confusion lies. For example, the following snippet works fine when I deploy:
import React from "react";
import "./NavbarLandingPage.css";
import logo from "./Inventory-Deals-LOGO-2021-White.png";
class NavbarLandingPage extends React.Component {
render () {
return (
<div>
<nav className="navbar navbar-expand-lg navhelp">
<a className="navbar-brand navlogo image" href="/">
<img
alt=""
className="responsive"
src={logo}
></img>
</a>
</nav>
</div>
)
}
}
export default NavbarLandingPage;
However, when I attempt to deploy it using "require()", it only shows a broken picture link, or nothing at all. For example, here is the exact same snippet using the require() method, yet does not work:
import React from "react";
import "./NavbarLandingPage.css";
class NavbarLandingPage extends React.Component {
render () {
return (
<div>
<nav className="navbar navbar-expand-lg navhelp">
<a className="navbar-brand navlogo image" href="/">
<img
alt=""
className="responsive"
src={require("./Inventory-Deals-LOGO-2021-White.png")}
></img>
</a>
</nav>
</div>
)
}
}
export default NavbarLandingPage;
One last thing to add - I ran an npm audit fix on vulnerabilities just before I began to take my HTML wireframes and convert them into React Components. One of the issues it wanted to fix caused "breaking changes", which I am sure wasn't a good thing...
Hope this provides enough information to let me know what I am doing wrong here. I would rather bring the images in using require(), as I would assume that would be the best way if rendering images dynamically through API calls, etc...
Thank you in advance for any and all help!
Could you try to use this instead
require("./Inventory-Deals-LOGO-2021-White.png").default
That fixed the issue for me.
i have simple react with redux app which uses bootstrap for only navbar but remaining website uses material-ui . i have to depend on bootstrap for only navbar because i could not find responsive navbar in material-ui documentation or any other resourse .so i used bootstrap cdn and used given below component which works fine .but its font size is small and text color is not so highlighted. more over background color for dropdown is also not so nice.
Moreover, i did not used style.css for styling because material-ui theme does not work together rather cssbaseline is used in material-ui. therefore i need material-ui working navbar component with dropdown and responsive toggle button .
so i want material-ui navbar with similar functionality .however ,if anyone can change this component in bootstrap with inline styling to make it fancy, it will be grateful.
here is MyNavbar.js component:
import React from 'react'
import {
BrowserRouter as Router,
Switch,
Route,
useParams,
} from "react-router-dom";
import { Navbar,Nav,NavDropdown,Form,FormControl,Button } from 'react-bootstrap'
import NeedForm from '../NeedForm';
import NewTutorForm from '../NewTutorForm';
class MyNavbar extends React.Component{
render(){
return(
<div>
<div className="row">
<div className="col-md-12">
<Navbar bg="primary" variant="dark" expand="lg">
<Navbar.Brand href="/">smart college</Navbar.Brand>
<img
src="src\assets\images\logo.svg"
width="30"
height="30"
className="d-inline-block align-top"
alt=" "></img>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/NeedForm">Hire a Tutor</Nav.Link>
<Nav.Link href="/NewTutorForm">Become Tutor</Nav.Link>
<NavDropdown bg="primary" title="Student Corner" id="basic-nav-dropdown">
<NavDropdown.Item href="courses"> courses</NavDropdown.Item>
<NavDropdown.Item href="Tutors">Tutors</NavDropdown.Item>
<NavDropdown.Item href="NeedForm">Hire a Tutor</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="LMS">My LMS</NavDropdown.Item>
</NavDropdown>
<NavDropdown title="Teacher's Corner" id="basic-nav-dropdown">
<NavDropdown.Item href="Needs">students Requests</NavDropdown.Item>
<NavDropdown.Item href="TutorDashBoard">Tutor DashBoard</NavDropdown.Item>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
</div>
</div>
)
}
}
export default MyNavbar;
here is screenshot of navbar:
You can use the App bar and Select Components to achieve your requirements.
Check the docs here:
https://material-ui.com/components/app-bar/#app-bar
https://material-ui.com/components/selects/#select
I created website in Gatsby (my first one) and I have trouble with the Gatsby's Link on the deployed page.
I am using a gatsby-starter-react-bootstrap which includes gatsby and react-bootstrap as the name says :)
I located Links in the NavDropdown.Item which is an element of the react-bootstrap.
import React from "react"
import {Link} from "gatsby"
import {Navbar, Nav, NavDropdown, Image} from "react-bootstrap"
import Logo from "../images/Logo_White_RGB_200x42px.png";
import customer_logo from "../images/customer_logo.svg";
const CustomNavbar = ({pageInfo}) => {
return (
<>
<Navbar variant="dark" expand="md" id="site-navbar">
{/* <Container> */}
<Link to="/" className="link-no-style">
<Navbar.Brand as="span">
<Image src={Logo} />
</Navbar.Brand>
</Link>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto" activeKey={pageInfo && pageInfo.pageName}>
<NavDropdown title="Project" id="collapsible-nav-dropdown">
<NavDropdown.Item><Link to="360-viewer" activeClassName="active">360 view</Link></NavDropdown.Item>
<NavDropdown.Item><Link to="map" activeClassName="active">map</Link></NavDropdown.Item>
<NavDropdown.Item><Link to="description" activeClassName="active">description</Link></NavDropdown.Item>
</NavDropdown>
</Nav>
<Nav className="ml-auto">
<Navbar.Text>
Customer: Customer Group <Image className="customer-logo" src={customer_logo}/>
</Navbar.Text>
</Nav>
</Navbar.Collapse>
{/* </Container> */}
</Navbar>
</>
)
};
export default CustomNavbar
For deployment I use https://www.npmjs.com/package/gh-pages.
Development version run localy on localhost:8000 works totaly fine. Dropdown and all of the links work perfectly.
Routing stops to work when I try to use version for production - gatsby build creates public folder where is index.html. Routing doesn't work also when I deploy page on the github pages.
Summary:
development version works fine
production and deployed version have problems :
when I click on the dropdown, the dropdown menu doesn't open and # sign is added to the URL address - www.website.com/#
when I add to the website address 360-viewer the page is opening but when I click again on the dropdown menu, the # sign is added again to the URL -www.website.com/360-viewer/#
Your application breaks in production with Github Pages because, unlike localhost, it's not served from the root URL. To fix this, you can let Gatsby know from which path your application will be served. Gatsby will then fix the routing and links for you.
In gatsby-config.js:
module.exports = {
pathPrefix: "/your-repo-name",
}
Then add --prefix-paths flag to your build command: gatsby build --prefix-paths
They explain this a bit more in their docs: https://www.gatsbyjs.org/docs/how-gatsby-works-with-github-pages/#deploying-to-a-path-on-github-pages
When I use NPM Start it opens up a webpage in Chrome but after a while, it says
"Aw, Snap! Something went wrong while displaying this webpage."
GitHub Repository for the code. I can't give a small piece fo code because I don't know what is giving the error.
I would like it to run and show my webpages. Not sure why it isn't working. The first time I started the server using NPM start it gave me this page.
The issue is with the Navbar.js
The issue is your class name here was Nav but Nav was also used in the render function. I have renamed Nav to CustomNavbar and imported the bootstrap nav from the npm package.
change your code to this
import React from 'react';
import { Link } from 'react-router-dom';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import { Button, FormControl, Form, NavDropdown } from "react-bootstrap";
export default class CustomNavbar extends React.Component {
render() {
return (
<div>
<Navbar bg="light" expand="lg">
<Navbar.Brand href="#home">Pawsitively Delicious</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/">Home</Nav.Link>
<Nav.Link href="AboutUs.js">About Us</Nav.Link>
<NavDropdown title="Ingredients" id="basic-nav-dropdown">
<NavDropdown.Item href="./TypesOfDogTreats">Types of Dog Treats</NavDropdown.Item>
<NavDropdown.Item href="AllIngredients.js">Ingredients</NavDropdown.Item>
</NavDropdown>
</Nav>
<Form inline>
<FormControl type="text" placeholder="Search" className="mr-sm-2" />
<Button variant="outline-success">Search</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</div>
);
} // END OF RENDER
} // END OF Navbar
I was having this issue, for me the problem was caused by adding a button component so i deleted a part of my codes
const checkButtonStyle = STYLES.includes(buttonStyle)?buttonStyle:STYLES[0]
const checkButtonSize = SIZE.includes(buttonSize) ? buttonSize: SIZE[0]
try this link for more options on how to solve it
1:
This question already has an answer here:
Cannot apply any Bootstrap style in using React-bootstrap library
(1 answer)
Closed 5 years ago.
I'm trying to make a navbar using react-bootstrap, but when I render it I get this:
I'm not getting any error messages, and I can't find any documentation explaining what I've got, but clearly I'm doing something wrong. Any ideas?
This is the code I'm using:
import React from 'react';
import { Navbar, Nav, NavItem } from 'react-bootstrap/lib/';
const Toolbar = () => {
return (
<Navbar staticTop collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
Brandname Goes Here
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1}>Contact Us</NavItem>
<NavItem eventKey={2}>About Us</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
export default Toolbar;
go to react-bootsrap getting started tab
and copy the link under the <!-- Latest compiled and minified CSS --> description into your index.html <head> tag, and if you installed react-bootstrap using npm, then you can import the navbar like so:
import { Navbar, Nav, NavItem } from 'react-bootstrap';
It is clear that you haven't imported bootstrap.min.css. Import it in your index.html as follows.
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous"
/>
I cannot recommend to use something like import 'react-bootstrap/*/*.css' because I am not sure which framework and which loaders(eg: webpack) you are using.