How can I remove the active Nav Item background feature in react-bootstrap when selecting Nav Item? It acting abnormally when I use it with 'react-redux' and 'react-router-bootstrap'?
For instance, when I reload the home page, the active background only stays on one of the Nav Items even if I select other Nav Items. Below is the image of the navitem (Dashboard) item is selected and the code for the navbar! Any recommendations would be greatly appreciate!
import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {LinkContainer, IndexLinkContainer} from 'react-router-bootstrap';
import {
Nav,
NavItem,
Navbar
} from 'react-bootstrap';
class NavBar extends Component {
render() {
const {authenticated} = this.props;
return (
<Navbar>
<Navbar.Header>
<Navbar.Brand>
Timesheet App
</Navbar.Brand>
<Navbar.Toggle/>
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<IndexLinkContainer to="/">
<NavItem className="nav-link" eventKey={1}>Dashboard</NavItem>
</IndexLinkContainer>
<LinkContainer to="/timesheet/new">
<NavItem className="nav-link" eventKey={2}>Submit Time</NavItem>
</LinkContainer>
<LinkContainer to="/user/Andriy">
<NavItem className="nav-link" eventKey={3}>Andriy Time</NavItem>
</LinkContainer>
{authenticated &&
<LinkContainer to="/signout">
<NavItem className="nav-link" eventKey={4}>Sign Out</NavItem>
</LinkContainer>}
{!authenticated &&
<LinkContainer to="/signin">
<NavItem className="nav-link" eventKey={5}>Sign In</NavItem>
</LinkContainer>}
{!authenticated &&
<LinkContainer to="/signup">
<NavItem className="nav-link">Sign Up</NavItem>
</LinkContainer>}
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
NavBar.propTypes = {
authenticated: PropTypes.bool
};
function mapStateToProps(state) {
return {
authenticated: state.auth.authenticated
};
}
export default connect(mapStateToProps)(NavBar);
If you want the active background gone, this can just be a CSS-only fix.
Just override the CSS for the active state background/border/etc to be the same as the non-active state.
Hope this helps.
You should create a class as a variable and use classNames to changed it based on whatever logic you decide to use.
You can then define something in CSS to deal with remove the active background.
For example (taken from the docs):
var btnClass = 'btn';
if (this.state.isPressed) btnClass += ' btn-pressed';
else if (this.state.isHovered) btnClass += ' btn-over';
return <button className={btnClass}>{this.props.label}</button>;
You can use different states to dictate what classes should be added and removed. This also helps with DRY principles as it will prevent you having to repeat yourself across all of the Navbar.
If you set activeKey="", then none of the links are active, and it's only the active key which gets a background.
<Nav activeKey="">
...
</Nav>
Related
I have a react app with a bootstrap offcanvas navbar that comes in from the right side when I click the hamburger. It works and closes when I click outside the offcanvas element or on the x inside the component, but when I click on a link it renders the new page but the navbar is still open. Is there a way I can force it to close when a user clicks on a link to a new page?
Here is the navbar code that is imported into the header which is imported into App.js:
import Container from 'react-bootstrap/Container';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import { LinkContainer } from 'react-router-bootstrap';
import Offcanvas from 'react-bootstrap/Offcanvas';
function OffcanvasNav() {
return (
<>
{[false].map((expand) => (
<Navbar key={expand} bg='light' expand={expand} className='mb-3'>
<Container fluid>
<Navbar.Brand to='/'>Denver Judo</Navbar.Brand>
<Navbar.Toggle aria-controls={`offcanvasNavbar-expand-${expand}`} />
<Navbar.Offcanvas
id={`offcanvasNavbar-expand-${expand}`}
aria-labelledby={`offcanvasNavbarLabel-expand-${expand}`}
placement='end'
>
<Offcanvas.Header closeButton>
<Offcanvas.Title id={`offcanvasNavbarLabel-expand-${expand}`}>
Denver Judo
</Offcanvas.Title>
</Offcanvas.Header>
<Offcanvas.Body>
<Nav className='justify-content-end flex-grow-1 pe-3'>
<LinkContainer to='/'>
<Nav.Link>Home</Nav.Link>
</LinkContainer>
<LinkContainer to='/schedule'>
<Nav.Link>Schedule</Nav.Link>
</LinkContainer>
<LinkContainer to='/about'>
<Nav.Link>About</Nav.Link>
</LinkContainer>
</Nav>
</Offcanvas.Body>
</Navbar.Offcanvas>
</Container>
</Navbar>
))}
</>
);
}
export default OffcanvasNav;
I don't know if this is the best way to handle it but I added an onClick to each of the LinkContainers to set expend = '' and it works.
<LinkContainer to='/' onClick={() => (this.expand = '')}>
<Nav.Link>Home</Nav.Link>
</LinkContainer>
<LinkContainer to='/schedule' onClick={() => (this.expand = '')}>
<Nav.Link>Schedule</Nav.Link>
</LinkContainer>
<LinkContainer to='/about' onClick={() => (this.expand = '')}>
<Nav.Link>About</Nav.Link>
</LinkContainer>
Navbar doesnt change a background color, i tried to do bg/variant = dark/black , it doesnt change anything
import React, { Component } from 'react';
import {Navbar,Container,Nav} from 'react-bootstrap';
export default class Header extends Component{
render() {
return(
< Navbar collapseOnSelect expand="lg" bg="dark" variant="dark" >
<Container>
<Navbar.Brand href="/">My landing Page</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav"/>
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/aboutMe" > About me</Nav.Link>
<Nav.Link href="/aboutUnivercity"> My Univercity</Nav.Link>
<Nav.Link href="/video"> My videos</Nav.Link>
<Nav.Link href="/myMotivate"> MyMotivate</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
)
}
}
Source if needed:
https://github.com/aokhar/SumPract
You have to add bootstrap in your react app.
In your terminal using yarn type:
yarn add bootstrap
Then at the top of your js file add:
import "bootstrap/dist/css/bootstrap.css";
After this hopefully all the colors will render.
these two screenshots are showing the same object, returned from a call to useStaticQuery, one in development and one in production. the development object looks the way its supposed to (as it appears in graphiql) but the image on top of production, the object is really weird with totally different fields, and I can't understand why.
to clarify, the first image is of production and the second is development build
Can someone please tell me why this is happening?
here is the whole component:
import React from "react"
import { linkResolver } from "../../utils/linkResolver"
import { useStaticQuery, graphql, Link } from "gatsby"
import Nav from "react-bootstrap/Nav"
import Navbar from "react-bootstrap/Navbar"
import NavDropdown from "react-bootstrap/NavDropdown"
const MyNav = () => {
const data = useStaticQuery(graphql`
query myNavQuery {
prismic {
allAffiliates {
edges {
node {
_meta {
id
type
uid
}
about
affiliate_name
author_name
image_1
image_2
map
}
}
}
}
}
`)
const affiliates = data.prismic.allAffiliates.edges
return (
<Navbar collapseOnSelect variant="light" expand="sm">
<Navbar.Brand href="/">
<div className="nav-logo">Global Paradise Studios</div>
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto">
<NavDropdown title="Affiliates" id="basic-navbar-dropdown">
{affiliates.map(affiliate => {
return (
<NavDropdown.Item href={linkResolver(affiliate.node._meta)}>
{affiliate.node.affiliate_name[0].text}
</NavDropdown.Item>
)
})}
</NavDropdown>
<Nav.Link as={Link} to="/">
Home
</Nav.Link>
<Nav.Link as={Link} to="/about">
About
</Nav.Link>
<Nav.Link as={Link} to="/contact">
Contact
</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
export default MyNav
Are you using the gatsby-source-prismic-graphql plugin? If so, then it doesn't currently support the useStaticQuery hook. As you've seen it will cause differences in development and prod.
In the README for the plugin you can see how to use the StaticQuery component instead which is supported and works.
https://github.com/birkir/gatsby-source-prismic-graphql#staticquery-and-usestaticquery
Give that a try!
The following code renders my NavLinks properly in my ReactStrap DropdownMenu:
<!-- RENDERS CORRECTLY -->
<Nav className="ml-auto" navbar>
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav caret> A dropdown menu </DropdownToggle>
<DropdownMenu>
<NavLink className="dropdown-item" to=“/url1”> item 1 </NavLink>
<NavLink className="dropdown-item" to=“/url2”> item 2 </NavLink>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
But if I move the DropdownMenu into a separate component as follows, returning exactly the same JSX, the css is screwy and the resulting a elements have "to" attributes instead of "href" attributes, so the links don't work.
<!-- BREAKS -->
<Nav className="ml-auto" navbar>
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav caret> A dropdown menu </DropdownToggle>
<DropdownMenuComponent/>
</UncontrolledDropdown>
</Nav>
...
class DropdownMenuComponent extends Component {
render() {
return (
<DropdownMenu>
<NavLink className="dropdown-item" to=“/url1”> item 1 </NavLink>
<NavLink className="dropdown-item" to=“/url2”> item 2 </NavLink>
</DropdownMenu>
);
}
}
Any ideas how I can fix this? It's disconcerting to use ReactStrap if I can't count on basic nesting of components.
You have to wrap the links in the <DropdownItem> component and then they'll render correctly.
I put together a working sandbox here
import React from "react";
import ReactDOM from "react-dom";
import {
Nav,
UncontrolledDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem,
NavLink
} from "reactstrap";
import "bootstrap-css-only";
const DropdownComponent = () => (
<DropdownMenu>
<DropdownItem>
<NavLink className="dropdown-item" to="/url1">
item 1
</NavLink>
</DropdownItem>
<DropdownItem>
<NavLink className="dropdown-item" to="/url2">
item 2
</NavLink>
</DropdownItem>
</DropdownMenu>
);
const App = () => {
return (
<Nav className="ml-auto" navbar>
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav caret>
A dropdown menu
</DropdownToggle>
<DropdownComponent />
</UncontrolledDropdown>
</Nav>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
I've used ReactStrap before, and from my experience, you often have to use Bootstrap classes in place of components if you start getting wonky styling attributes.
Also, in your second example when the code breaks, your Nav component isn't going to render to the DOM since it's not being returned inside of the render() life cycle method. You're going to need to place it inside the existing DropdownMenuComponent render method or create a new component for the Nav and import it into the parent component in order for it to render properly.
Hope this helps!
I am using bootstrap 4.0 in React. Just plain bootstrap:
import React from "react";
import ReactDOM from "react-dom";
import "bootstrap/dist/css/bootstrap.css";
How can I make it work properly with React. When I use mobile and medium screen size it is appearing but it does not work. What is the proper way in this situation? Create local state with toggle manually?
Watch how it works here: project
Here is an example from
https://reactstrap.github.io/components/navbar/
this is using reactstrap but may help you understand how to do it in react
import React from 'react';
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.toggleNavbar = this.toggleNavbar.bind(this);
this.state = {
collapsed: true
};
}
toggleNavbar() {
this.setState({
collapsed: !this.state.collapsed
});
}
render() {
return (
<div>
<Navbar color="faded" light>
<NavbarBrand href="/" className="mr-auto">reactstrap</NavbarBrand>
<NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
<Collapse isOpen={!this.state.collapsed} navbar>
<Nav navbar>
<NavItem>
<NavLink href="/components/">Components</NavLink>
</NavItem>
<NavItem>
<NavLink href="https://github.com/reactstrap/reactstrap">GitHub</NavLink>
</NavItem>
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}