Reactstrap Bootstrap Navbar not working in react app
Asked Answered
K

5

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?

Kropotkin answered 4/2, 2019 at 16:28 Comment(2)
you need to import the css import 'bootstrap/dist/css/bootstrap.min.css'; as stated in the docs reactstrap.github.ioSalisbarry
Thanks! This works. I added bootstrap too. I was not aware I should do it additionally while using Reactstrap. Thanks for the help!Kropotkin
B
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'; 
Bedspread answered 2/5, 2019 at 10:1 Comment(0)
S
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.

Septate answered 16/4, 2020 at 21:18 Comment(0)
F
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>
Fright answered 31/3, 2019 at 12:8 Comment(0)
S
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>
  );
};
Sulk answered 15/2, 2021 at 15:17 Comment(0)
U
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

Utility answered 20/8, 2023 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.