Michael Kitas
Michael Kitas

Reputation: 249

Bootsrap button don't work properly in React

I am building a responsive navigation bar in React and when you zoom in to the page it hides the links and shows a button with the drop-down list with the link.

The thing is when I click a button it doesn't do anything and in a bootstrap website, it functions correctly.

Have I done anything wrong?

Here is my code:

import React, { Component } from 'react';
import axios from "axios";
import { Redirect } from "react-router-dom"
import styles from '../styles/loginsignup.css'
import logo from '../img/nowyourguest.png'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/dist/jquery.min.js'
import 'bootstrap/dist/js/bootstrap.min.js'

export default class Login extends Component {
  state = {
    email: '',
    password: ''
  };

  handleSubmit = event => {
    event.preventDefault();
    const {email, password} = this.state;
    axios({
      url: "/authentication/signin",
      method: "POST",
      data: {
        email,
        password
      }
    })
      .then(response => {
        const isAuthenticated = response.data.isAuthenticated
        window.localStorage.setItem('isAuthenticated', isAuthenticated);
        this.props.history.push('/profile')
      })
      .catch(error => {
        this.setState({
          errorMessage: error.response.data.message
        })
      })
  };

  handleChange = event => {
    const {name, value} = event.target;
    this.setState({
      [name]: value
    })
  }

  render() {
    const isAuthenticated = window.localStorage.getItem('isAuthenticated');
    if (isAuthenticated) {
      return <Redirect to='/profile'/>
    }

    return (
      <div>
        <nav className="navbar navbar-expand-lg navbar-light bg-light">
          <a className="navbar-brand" href="#">Navbar</a>
          <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                  aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span className="navbar-toggler-icon"/>
          </button>
          <div className="collapse navbar-collapse" id="navbarSupportedContent">
            <ul className="navbar-nav mr-auto">
              <li className="nav-item active">
                <a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
              </li>
              <li className="nav-item">
                <a className="nav-link" href="#">Link</a>
              </li>
              <li className="nav-item dropdown">
                <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
                   data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Dropdown
                </a>
                <div className="dropdown-menu" aria-labelledby="navbarDropdown">
                  <a className="dropdown-item" href="#">Action</a>
                  <a className="dropdown-item" href="#">Another action</a>
                  <div className="dropdown-divider"/>
                  <a className="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
              <li className="nav-item">
                <a className="nav-link disabled" href="#">Disabled</a>
              </li>
            </ul>
            <form className="form-inline my-2 my-lg-0">
              <input className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"/>
              <button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
            </form>
          </div>
        </nav>
      </div>
    )
  }
}

module.exports = Login;

Upvotes: 1

Views: 58

Answers (3)

Michael Kitas
Michael Kitas

Reputation: 249

So I had to import jquery in index.html so this is how I imported it:

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
</head>

<body>
    <div id="root"></div>


    <!-- Turbo library imports: jQuery, Turbo CDN, sample app.js -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
    <script type="text/javascript" src="/dist/js/vendor.min.js"></script>
    <script type="text/javascript" src="https://cdn.turbo360-dev.com/dist/turbo.min.js"></script>
    <script type="text/javascript" src="/dist/bundle/commons.js"></script>
    <script type="text/javascript" src="/dist/bundle/app.js"></script> <!-- React code bundle -->
</body>
</html>

Upvotes: 0

Nisharg Shah
Nisharg Shah

Reputation: 19662

That button not working because it depends on jQuery, for that you need to install jQuery in your code, but I don't suggest you install jQuery on your code.

For that, you can use react-bootstrap

<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span className="navbar-toggler-icon" />
</button>

Upvotes: 1

Sarthak Aggarwal
Sarthak Aggarwal

Reputation: 2312

You need to connect your button click with its handler

Basic usage assuming you use Stateful or Class-based component.

const onClickHandler = (e) =>{
     console.log(e);
}

render(){
  <button onClick={this.onClickHandler}>Click Me!</button>
}

Upvotes: 1

Related Questions