13

I created a react app with 'create react-app'.

I am trying now to add a navbar from Reactstrap. I do copy-paste from Reactstrap (I am adding this as one react component) website:

import React from 'react';
import {
  Collapse,
  Navbar,
  NavbarToggler,
  NavbarBrand,
  Nav,
  NavItem,
  NavLink,
  UncontrolledDropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem } from 'reactstrap';

export default class Example extends React.Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      isOpen: false
    };
  }
  toggle() {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }
  render() {
    return (
      <div>
        <Navbar color="light" light expand="md">
          <NavbarBrand href="/">reactstrap</NavbarBrand>
          <NavbarToggler onClick={this.toggle} />
          <Collapse isOpen={this.state.isOpen} navbar>
            <Nav className="ml-auto" navbar>
              <NavItem>
                <NavLink href="/components/">Components</NavLink>
              </NavItem>
              <NavItem>
                <NavLink href="https://github.com/reactstrap/reactstrap">GitHub</NavLink>
              </NavItem>
              <UncontrolledDropdown nav inNavbar>
                <DropdownToggle nav caret>
                  Options
                </DropdownToggle>
                <DropdownMenu right>
                  <DropdownItem>
                    Option 1
                  </DropdownItem>
                  <DropdownItem>
                    Option 2
                  </DropdownItem>
                  <DropdownItem divider />
                  <DropdownItem>
                    Reset
                  </DropdownItem>
                </DropdownMenu>
              </UncontrolledDropdown>
            </Nav>
          </Collapse>
        </Navbar>
      </div>
    );
  }
}

and it does not work. I am getting this in my react app:

navbar in react app

I had similar problems with Bootstrap, React and Navbar before but I am really surprised it is happening in Reactstrap. Do you know why is it so?

Talita
  • 805
  • 3
  • 11
  • 31
  • 8
    you need to import the css `import 'bootstrap/dist/css/bootstrap.min.css';` as stated in the docs https://reactstrap.github.io/ – Sebastiaan van Arkens Feb 04 '19 at 16:29
  • 1
    Thanks! This works. I added bootstrap too. I was not aware I should do it additionally while using Reactstrap. Thanks for the help! – Talita Feb 04 '19 at 16:34

5 Answers5

18

You need also bootstrap, and react-dom:

npm install --save bootstrap
npm install --save reactstrap react react-dom

import 'bootstrap/dist/css/bootstrap.css';
import { Button } from 'reactstrap'; 
Matthijs
  • 2,483
  • 5
  • 22
  • 33
Andrew Kazanin
  • 196
  • 1
  • 2
6

You should make sure you have

import 'bootstrap/dist/css/bootstrap.css';

imported into the index.js file created automatically when you create react app.

Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41
Josh Eshleman
  • 61
  • 1
  • 3
2

You can do this.

import { NavLink } from 'reactstrap';
import { NavLink as ReactLink } from 'react-router-dom';

<NavLink tag={ReactLink} to="/home" activeClassName="active" >Home</NavLink>
cptiwari20
  • 442
  • 1
  • 7
  • 18
0

While it's not documented, you have to use as follows with react-router:

<NavLink tag={Link} to="/somewhere">

IE:

import React from 'react';
import { Link } from 'react-router';
import { NavLink } from 'reactstrap';

const Example = (props) => {
  return (
    <NavLink tag={Link} to="/somewhere">
      {props.item.name}
    </NavLink>
  );
};
maroof shittu
  • 2,027
  • 1
  • 11
  • 14
0

just install

npm install bootstrap
npm install jquery 

then import them in index.js

import 'bootstrap/dist/css/bootstrap.min.css';
import "jquery/dist/jquery.min.js";
import 'bootstrap/dist/js/bootstrap.bundle'

it worked for me while i am fail to toggle.

for more fetures you can install popper as well