I have recently migrated from using twitter-bootstrap in react classes to using react-bootstrap. I wanted to test out react-bootstrap Navbar. My code is as:
import React from 'react';
import { Link } from 'react-router';
import Radium from 'radium';
import Grid from 'bootstrap-grid-react';
import * as bootstrap from "react-bootstrap";
export default class Layout extends React.Component {
constructor() {
super();
this.state = {};
}
render() {
let {Nav, Navbar, NavItem, NavDropdown, MenuItem} = bootstrap;
return (
<div>
<Navbar>
<Navbar.Header>
<Navbar.Brand>
APITest
</Navbar.Brand>
</Navbar.Header>
<Nav>
<NavItem eventKey={1} href="#">New user
</NavItem>
<NavItem eventKey={2} href="#">Create New User</NavItem>
<NavDropdown eventKey={3} title="Dropdown" id="basic-nav-dropdown">
<MenuItem eventKey={3.1}>Action</MenuItem>
<MenuItem eventKey={3.2}>Another action</MenuItem>
<MenuItem eventKey={3.3}>Something else here</MenuItem>
<MenuItem divider />
<MenuItem eventKey={3.3}>Separated link</MenuItem>
</NavDropdown>
</Nav>
</Navbar>
</div>
)
}
}
However this gives me an error
Uncaught Error: Invariant Violation: Element type is invalid:
expected a string (for built-in components) or a class/function (for
composite components) but got: undefined. Check the render method of
Layout.
and a warning:
Warning: React.createElement: type should not be null, undefined,
boolean, or number. It should be a string (for DOM elements) or a
ReactClass (for composite components). Check the render method of
Layout.
I also tried using
import {Nav, Navbar, NavItem, NavDropdown, MenuItem} from 'react-bootstrap' but it also won't work. There is definitely a problem in how I am using Navbar because a normal div is rendering just fine.
Any help is really appreciated.
After a long time struggling with it, I finally got it to work the problem being the extra div above the Navbar.
This is my Final working code:
import React from 'react';
import { Link } from 'react-router';
import Radium from 'radium';
import Grid from 'bootstrap-grid-react';
import {Nav, Navbar, NavItem, NavDropdown, MenuItem} from "react-bootstrap";
export default class Layout extends React.Component {
constructor() {
super();
this.state = {};
}
render() {
return (
<Navbar>
<Navbar.Header>
<Navbar.Brand>
APITest
</Navbar.Brand>
</Navbar.Header>
<Nav>
<NavItem eventKey={1} href="#">New user
</NavItem>
<NavItem eventKey={2} href="#">Create New User</NavItem>
<NavDropdown eventKey={3} title="Dropdown" id="basic-nav-dropdown">
<MenuItem eventKey={3.1}>Action</MenuItem>
<MenuItem eventKey={3.2}>Another action</MenuItem>
<MenuItem eventKey={3.3}>Something else here</MenuItem>
<MenuItem divider />
<MenuItem eventKey={3.3}>Separated link</MenuItem>
</NavDropdown>
</Nav>
</Navbar>
)
}
}
I still don't know the reason for an error when wrapping the Navbar in a div. Feel free to comment for any explanation.
After dealing with this for way too long, I've found that the problem is the Link component you're using from react-router. I'm not exactly sure on the specifics of the error, but there is a solution. Instead of using Link use the LinkContainer component provided by react-bootstrap-router https://github.com/react-bootstrap/react-router-bootstrap.
From the react-router-bootstrap docs:
npm i -S react-router-bootstrap
In your code use:
<LinkContainer to={{ pathname: '/foo', query: { bar: 'baz' } }}>
<Button>Foo</Button>
</LinkContainer>
Related
I have been trying to put a Navbar into a React Project using reactstrap. But the Navbar is not showing just the h1 in the NavbarBrand is rendered for some reasons. I tried to change the css styling, copy and pasted other example Navbars from different websites and tried other kinda similar solutions from here but nothing worked. I am using React version 17.0.2 and reactstrap version 8.9.0.
This is my Navbar Component:
import React, { Component } from 'react';
import { Navbar, NavbarBrand, Nav, NavbarToggler, Collapse, NavItem } from 'reactstrap';
//import { NavLink } from 'react-router-dom';
class Header extends Component {
constructor(props) {
super(props);
this.state = {
isNavOpen: false
};
this.toggleNav = this.toggleNav.bind(this);
}
toggleNav() {
this.setState({
isNavOpen: !this.state.isNavOpen
});
}
render() {
return(
<Navbar bg="dark" variant="dark">
<div className="container">
<NavbarToggler onClick={this.toggleNav} />
<NavbarBrand href="/">
<h1>Navbar</h1>
</NavbarBrand>
<Collapse isOpen={this.state.isNavOpen} navbar>
<Nav navbar>
<NavItem>
<p>Test</p>
</NavItem>
</Nav>
</Collapse>
</div>
</Navbar>
);
}
}
export default Header;
How the navbar looks
I am rather new to React and Javascript overall i hope someone can help me.
You have the isNavOpen in state set to false and your
<Collapse isOpen={this.state.isNavOpen} navbar>
<Nav navbar>
<NavItem>
<p>Test</p>
</NavItem>
</Nav>
</Collapse>
depends upon isNavOpen state which expects to be true to be shown on the screen.
Try setting isNavOpen to true in the state.
I'm building a navbar for a project and the above message is telling me to check the render method in my header component. I've tried everything I can think of, including looking through answers on here. Specifically, I tried moving the Header component between my App component and the index.js file Not sure if I need a break or something but in any case, here's my header:
import React from "react";
import Nav from "react-bootstrap/Nav";
import Navbar from "react-bootstrap/Navbar";
import "bootstrap/dist/css/bootstrap.min.css";
import LinkContainer from "react-router-bootstrap";
const header = () => {
return (
<Navbar collapseOnSelect expand="lg">
<LinkContainer to="/App">
<Navbar.Brand href="/App">
<img
src="/PPLogoEdit.jpg"
width="110"
height="50"
className="d-inline-block align-top"
alt="pikelogo"
/>
</Navbar.Brand>
</LinkContainer>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<LinkContainer to="/App">
<Nav.Link style={{ color: "yellow" }} href="/App">
Home
</Nav.Link>
</LinkContainer>
<LinkContainer to="/About">
<Nav.Link style={{ color: "orange" }} href="/About">
About
</Nav.Link>
</LinkContainer>
<LinkContainer to="/Gallery">
<Nav.Link style={{ color: "green" }} href="/Gallery">
Gallery
</Nav.Link>
</LinkContainer>
<LinkContainer to="/Shop">
<Nav.Link style={{ color: "blue" }} href="/Shop">
Shop
</Nav.Link>
</LinkContainer>
<LinkContainer to="Contact">
<Nav.Link style={{ color: "purple" }} href="/Contact">
Contact
</Nav.Link>
</LinkContainer>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
export default header
and the index.js file:
import React from "react";
import ReactDOM from "react-dom";
import 'bootstrap/dist/css/bootstrap.min.css'
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Header from './components/Header';
import App from "./components/App";
import About from "./components/About";
import Gallery from "./components/Gallery";
import Shop from "./components/Shop";
import Contact from "./components/Contact";
ReactDOM.render(<React.StrictMode>
<Router>
<Switch>
<Route exact path="/" component={App} />
<Route path="/about" component={About} />
<Route path="/gallery" component={Gallery} />
<Route path="/shop" component={Shop} />
<Route path="/contact" component={Contact} />
</Switch>
</Router>
</React.StrictMode>, document.getElementById("root"));
Any help is appreciated, thank you!
Look at the error messages:
Warning: React.createElement: type is invalid -- expected a
string (for built-in components) or a class/function (for composite
components) but got: undefined. You likely forgot to export your
component from the file it's defined in, or you might have mixed up
default and named imports.
Check your code at Header.jsx:24.
in Header (at src/index.js:15)
in Router (created by BrowserRouter)
in BrowserRouter (at src/index.js:14)
in StrictMode (at src/index.js:13)
Repeats "Check your code at Header.jsx:XX" for all lines where LinkContainer is used.
It's flagging the LinkContainer you try to import from react-router-bootstrap. You default import it but it should be a named import.
import { LinkContainer } from "react-router-bootstrap";
I'm fairly new to ReactJS and Bootstrap, so I apologize if this is a dumb question. I downloaded a template online and I have a dropdown in the navbar of my app. For reference, here is what the dropdown looks like:
What I want is for each <MenuItem> (The 'API Service #' buttons) to send me to a different link. I have tried tweaking around a little bit and this is what I got so far:
import React, { Component } from "react";
import { Nav, NavDropdown, MenuItem } from "react-bootstrap";
class AdminNavbarLinks extends Component {
render() {
return (
<div>
<Nav>
<NavDropdown eventKey={2} title="Dropdown" id="basic-nav-dropdown">
<MenuItem eventKey={2.1} href="/serviceone">
API Service 1
</MenuItem>
<MenuItem eventKey={2.2} href="/servicetwo">
API Service 2
</MenuItem>
<MenuItem eventKey={2.3} href="/servicethree">
API Service 3
</MenuItem>
<MenuItem eventKey={2.4} href="/servicefour">
API Service 4
</MenuItem>
<MenuItem eventKey={2.5} href="/servicefive">
API Service 5
</MenuItem>
</NavDropdown>
</Nav>
<Nav pullRight>
//Other NavDropdowns
</Nav>
</div>
);
}
}
export default AdminNavbarLinks;
What this code currently does is sends you to the link that corresponds to each href, but then immediately sends you to the default page of the app, which is /dashboard (An example is if I click on the "API Service 3" button on the dropdown, it sends me to "#/servicethree", but then immediately sends me back to "#/dashboard"). I'm curious as to why it reverts me back to the default page? Any help would be appreciated, since i'm not quite sure how to make these MenuItems have a link. I can provide more code if necessary.
EDIT:
Here is the updated AdminNavbarLinks file:
import React, { Component } from "react";
import { Nav, NavDropdown, MenuItem } from "react-bootstrap";
import { BrowserRouter as Link } from "react-router-dom";
class AdminNavbarLinks extends Component {
render() {
return (
<div>
<Nav>
<NavDropdown eventKey={2} title="Dropdown" id="basic-nav-dropdown">
<Link to="/serviceone">
<MenuItem eventKey={2.1} href="/serviceone">
API Service 1
</MenuItem>
</Link>
<MenuItem eventKey={2.2} href="/servicetwo">
API Service 2
</MenuItem>
<MenuItem eventKey={2.3} href="/servicethree">
API Service 3
</MenuItem>
<MenuItem eventKey={2.4} href="/servicefour">
API Service 4
</MenuItem>
<MenuItem eventKey={2.5} href="/servicefive">
API Service 5
</MenuItem>
</NavDropdown>
</Nav>
<Nav pullRight>
//Other NavDropdowns
</Nav>
</div>
);
}
}
export default AdminNavbarLinks;
And now here is the main file index.js:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter, Route, Switch, Redirect } from "react-router-dom";
import Navbar from "components/Navbars/AdminNavbarLinks.jsx";
import Dashboard from "views/Dashboard.jsx";
import ServiceOne from "views/ServiceOne.jsx";
import "bootstrap/dist/css/bootstrap.min.css";
import "./assets/css/animate.min.css";
import "./assets/sass/light-bootstrap-dashboard-react.scss?v=1.3.0";
import "./assets/css/demo.css";
import "./assets/css/pe-icon-7-stroke.css";
import AdminLayout from "layouts/Admin.jsx";
ReactDOM.render(
<BrowserRouter>
<Navbar />
<Switch>
<Route path="/" component={Dashboard} exact />
<Route path="/serviceone" component={ServiceOne} exact />
</Switch>
<Switch>
<Route path="/admin" render={props => <AdminLayout {...props} />} />
<Redirect from="/" to="/admin/dashboard" />
</Switch>
</BrowserRouter>,
document.getElementById("root")
);
EDIT #2
Solution found. See my answer below.
I finally got this to work. All I did was change some properties of the <Link to tag. Here is how AdminNavbarLinks now looks:
import React, { Component } from "react";
import { Nav, NavDropdown, MenuItem } from "react-bootstrap";
import { NavLink } from "react-router-dom";
class AdminNavbarLinks extends Component {
render() {
return (
<div>
<Nav>
<NavDropdown title="Services" id="basic-nav-dropdown">
<MenuItem>
<NavLink
to="/admin/serviceone"
className="nav-link"
activeClassName="active"
>
API Service 1
</NavLink>
</MenuItem>
<MenuItem>
<NavLink
to="/admin/servicetwo"
className="nav-link"
activeClassName="active"
>
API Service 2
</NavLink>
</MenuItem>
<MenuItem>
<NavLink
to="/admin/servicethree"
className="nav-link"
activeClassName="active"
>
API Service 3
</NavLink>
</MenuItem>
<MenuItem>
<NavLink
to="/admin/servicefour"
className="nav-link"
activeClassName="active"
>
API Service 4
</NavLink>
</MenuItem>
<MenuItem>
<NavLink
to="/admin/servicefive"
className="nav-link"
activeClassName="active"
>
API Service 5
</NavLink>
</MenuItem>
</NavDropdown>
</Nav>
<Nav pullRight>
//Other NavDropdowns
</Nav>
</div>
);
}
}
export default AdminNavbarLinks;
You need to integrate react-router-dom to navigate across components in your React application.
Simply nest the MenuItems in a Link tag. And in each Link tag, you define the to property with the path that you want to navigate to. This will end up displaying a completely different component.
import React, { Component } from "react";
import { Nav, NavDropdown, MenuItem } from "react-bootstrap";
class AdminNavbarLinks extends Component {
render() {
return (
<div>
<Nav>
<NavDropdown eventKey={2} title="Dropdown" id="basic-nav-dropdown">
<Link to="/serviceone">
<MenuItem eventKey={2.1}>
API Service 1
</MenuItem>
</Link>
...repeat for others
</NavDropdown>
</Nav>
<Nav pullRight>
//Other NavDropdowns
</Nav>
</div>
);
}
}
export default AdminNavbarLinks;
You would also have to define your Routes in your main application, to complete the configuration.
Something like this would suffice:
App.js
import React from "react"
import { BrowserRouter, Route, Switch } from "react-dom
import Navbar from "whatever path this"
import Home from "./whatever path this"
import ServiceOne from "./whatever path this"
const App = () => {
return(
<BrowserRouter>
<Navbar/>
<Switch>
<Route path="/" component={Home} exact/>
<Route path="/serviceone" component={ServiceOne} exact/>
</Switch>
</BrowserRouter>
)
}
In the above we've defined some very basic routing for your App. I'm assuming your Navbar actually has the AdminNavbarLinks component inside it. From there you just click a Link with a matching url path to the Routes you defined.
I have 4 files: App.js, navbar.js, signIn.js, signUp.js. I expect the navbar to be displayed at the top of each page that the user navigates. The links in the navbar work on the homepage but the links are disabled on any other page.
I've tried installing react-bootstrap through npm instead of using the cdn but that did not change the issue. I tried creating another group of components called "NavigationLinks.js" but that didn't work either. I'm at a loss as to what else I could do.
Here are the relevant files:
App.js
import logo from './logo.svg';
import './App.css';
import Navbar from './components/layout/navbar'
import Dashboard from './components/feed/dashboard'
import SignIn from './components/auth/signIn'
import SignUp from './components/auth/signUp'
import { BrowserRouter, Switch, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<div className="App">
<Navbar/>
<Switch>
<Route exact path="/" component={Dashboard}/>
<Route path="/signin" component={SignIn}/>
<Route path="/signup" component={SignUp}/>
</Switch>
</div>
</BrowserRouter>
);
}
export default App;
Navbar.js
import { Switch, Route, Link} from 'react-router-dom';
import { Navbar, Nav, Form, FormControl, Button, NavItem } from 'react-bootstrap';
//import './navbar.css';
class navbar extends Component {
render() {
return (
<div>
<div>
<Navbar>
<Navbar.Brand as={Link} to='/'>Rekindle</Navbar.Brand>
<Navbar.Collapse>
<Nav className="mr-auto">
<NavItem eventkey={1} href="/">
<Nav.Link as={Link} to="/" >Home</Nav.Link>
</NavItem>
<NavItem eventkey={1} href="/signup">
<Nav.Link as={Link} to="/signup" >Sign Up</Nav.Link>
</NavItem>
<NavItem eventkey={1} href="/signin">
<Nav.Link as={Link} to="/signin" >Login</Nav.Link>
</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
{/*this div works for the routing */}
<div>
</div>
</div>
);
}
}
export default navbar;
The code compiles without errors, but I want to be able to navigate back to the homepage from the signIn and signUp routes.
In your code snippet I found some issue, like you missed to import react only.
Any file you create, you need to import react like,
import React from 'react'; //default
Also you are extending Component in your navbar component which is also need to import from react package.
2 ways to extend Component.
Way 1,
import React from 'react';
class navbar extends React.Component{
render(){
return(
<div>...</div>
)
}
}
export default navbar;
Way 2,
import React,{Component} from 'react';
class navbar extends Component{
render(){
return(
<div>...</div>
)
}
}
export default navbar;
You missed this in you navbar component.
Here is the working code snippet for you.
It looks like these integration errors are common and you are suppose to use: https://github.com/react-bootstrap/react-router-bootstrap
It works when I give the part inside app.js but it shows blank screen when it is put in another file navbar.js and imported. If the code in navbar.js is directly added in app.js instead of importing, it works fine and navbar appears. Otherwise blank screen appears.
index.js is shown below:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root')
);
app.js file
import React, { Component } from 'react';
import navbar from './navbar.js';
class home extends Component {
render() {
return (
<div>
<navbar />
</div>
);
}
}
export default home;
navbar.js file:
import React, { Component } from 'react';
import {Navbar} from 'react-bootstrap';
import {NavItem} from 'react-bootstrap';
import {Nav} from 'react-bootstrap';
class navbar extends Component{
render() {
return (
<Navbar collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
React-Bootstrap
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<NavItem eventKey={1} href="#">Link</NavItem>
<NavItem eventKey={2} href="#">Link</NavItem>
</Nav>
<Nav pullRight>
<NavItem eventKey={1} href="#">Link Right</NavItem>
<NavItem eventKey={2} href="#">Link Right</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
);
}
}
export default navbar;
Please help, thanks in advance.
You should always start the name of your custom React components with
a uppercase letter.
import React, { Component } from 'react';
import Navbar from './Navbar.js';
class Home extends Component {
render() {
return (
<div>
<Navbar />
</div>
);
}
}
export default Home;
and it's recommended to use the same name for the file name as well. So it should be ./Navbar.js instead of ./navbar.js
Read this page from the official documentation for more information.
Always start component names with a capital letter. For example, <div/> represents a DOM tag, but <Welcome /> represents a component and
requires Welcome to be in scope.