I'm trying to build a Dropdown component in React with Bootstrap styling without using a library like react-bootstrap or reactstrap.
I'm a little confused on how to implement the bootstrap javascript into a React component.
I suspect I need to use refs to instantiate a new Dropdown class, but with what I have below the dropdown does nothing and there are no errors in console.
import React, { useRef, useEffect } from 'react'
import '#popperjs/core'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.js'
import { Dropdown } from 'bootstrap'
function App() {
const dropdownRef = useRef<any>(null)
useEffect(() => {
if (dropdownRef.current) {
new Dropdown(dropdownRef.current, {
autoClose: 'inside'
})
}
}, [dropdownRef.current])
return (
<div>
<div className="dropdown">
<button
className="btn btn-secondary dropdown-toggle"
type="button"
data-bs-toggle="dropdown"
aria-expanded="false"
ref={dropdownRef}
>
Dropdown button
</button>
<ul className="dropdown-menu dropdown-menu-dark">
<li><a className="dropdown-item active" href="#">Action</a></li>
<li><a className="dropdown-item" href="#">Another action</a></li>
<li><a className="dropdown-item" href="#">Something else here</a></li>
<li><hr className="dropdown-divider" /></li>
<li><a className="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
</div>
);
}
What am I missing here? The docs are pretty sparse on how to implement the javascript.
Related
I've been using Bootstrap 5 for a while not noticing something may not work.
Then I found a model requires #popper/core, not just popper.js.
I installed it and suddenly my dropdowns stopped working.
The context: I use the dropdown in my React JSX layout.
React 16.11.0
Bootstrap 5.1.3
Some code:
import Popper from '#popperjs/core';
import $ from 'jquery';
//import { Dropdown } from 'bootstrap'; <-- doesn't help
import 'bootstrap/dist/js/bootstrap.bundle.min';
...
<div className="dropdown">
<button className="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul className="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a className="dropdown-item" href="#">Action</a></li>
<li><a className="dropdown-item" href="#">Another action</a></li>
<li><a className="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
It started working after I removed the "flat" reference to the bootstrap:
import 'bootstrap/dist/js/bootstrap.bundle.min';
From here:
https://gitlab.com/simevo/dropdown/-/commit/624e010e7aaba574bbab4af431054d4c5e47e80e
Updated, now the problem i'm seeing the navbar with format problems, shouldn't it be taking the CSS properties from bootstrap?
I Commited the project to make it easier: https://github.com/damianaguilarcogan/pfi_front_v4
Image: https://ibb.co/fdFTb4C
i just created a project but the navbar it's not loading, for sure it's a silly mistake but i'm not beeing able to find it.
I created the project, imported bootstrap in the index.html trought css links.
I created the folder components and inside ir navbar.js.
Afterwards I selected a common template from bootstrap navbar and Finally y called it from my app.js.
I don't get to see whats wrong, it should have already worked with this steps.
app.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import navbar from './components/navbar';
function App() {
return (
<div className="App">
<navbar />
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
navbar.js
import React from 'react';
class navbar extends React.Component {
render() {
return (
<nav className="navbar navbar-toggleable-md navbar-light bg-faded">
<button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<a className="navbar-brand" href="#">Navbar</a>
<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">
<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="text" placeholder="Search"></input>
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
)
}
}
export default navbar;
[1]: https://i.stack.imgur.com/zk8Lf.png
React expects components to be named with an uppercase. Try renaming the navbar to Navbar.
class Navbar extends React.Component
<Navbar/>
Changing your navbar to Navbar works. In addition there is a bootstrap wrapper for React that can be useful if you are just starting now. https://react-bootstrap.github.io/
I'm trying to put a dropdown menu in my navbar. I'm using the materialize framework, and so far this is the only materialize javascript element I can't get to work. Here's the code I am using for the navbar:
import React, { Component } from "react";
import M, { Dropdown } from "materialize-css";
class Navbar extends Component {
componentDidMount() {
M.Dropdown.init(this.Dropdown);
}
render() {
return (
<div className="navbar-fixed">
<nav>
<div class="nav-wrapper purple">
<a href="/" class="brand-logo">
Logo
</a>
<a class="dropdown-trigger btn right" data-target="dropdown1">
Drop Me!
</a>
<ul
id="dropdown1"
class="dropdown-content"
ref={Dropdown => {
this.Dropdown = Dropdown;
}}
>
<li>
one
</li>
<li>
two
</li>
<li class="divider" tabindex="-1" />
<li>
three
</li>
<li>
<a href="#!">
<i class="material-icons">view_module</i>four
</a>
</li>
<li>
<a href="#!">
<i class="material-icons">cloud</i>five
</a>
</li>
</ul>
</div>
</nav>
</div>
);
}
}
export default Navbar;
Essentially I click the element and nothing happens. Not sure what I'm doing wrong, and I can't find anything similar on Stack overflow. Here's the code sandbox... https://codesandbox.io/s/dawn-snow-3cmdv
Thanks!
I include a dropdown button to React from bootstrap Dropdowns, But it is not working and shows as a normal button. Can you give me a solution for this?
<div className="dropdown">
<button className="btn btn-secondary
dropdown-toggle"
type="button"
id="dropdownMenuButton"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
Dropdown button
</button>
<div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a className="dropdown-item" href="#">Action</a>
<a className="dropdown-item" href="#">Another action</a>
<a className="dropdown-item" href="#">Something else here</a>
</div>
</div>
The output is a normal button like this.
Dropdowns are not working without popper.js and jquery.js.
So please install popper.js and jquery.js in your system using npm install popper.js jquery --save and don't forget to include it.
With CDN
https://stackblitz.com/edit/create-react-class-m3qfxu?file=index.html
With NPM
https://stackblitz.com/edit/create-react-class-xvsthx?file=index.js
If someone doesn't want to install other dependencies, they can make it work using react useState hook.
import React, { useState } from 'react';
export default function DropCard() {
const [dropdown, setDropdown] = useState(false);
const toggleOpen = () => setDropdown(!dropdown);
return (
<div className="dropdown">
<button onClick={toggleOpen}>
Dropdown
</button>
<div
className={`dropdown-menu ${dropdown ? 'show' : ''}`}
aria-labelledby="dropdownMenuButton"
>
<a className="dropdown-item" href="#">
Delete
</a>
<a className="dropdown-item" href="#">
Pin to your Profile
</a>
</div>
</div>
);
}
Make sure you have the bootstrap js imported correctly
npm install --save bootstrap
then import "bootstrap/dist/js/bootstrap.min.js";
This worked for me.
To solve this issue need to pass rootCloseEvent, for example
<DropdownButton
title="Download"
rootCloseEvent="mousedown"
>
<MenuItem
onClick={}
>
PDF
</MenuItem>
<MenuItem
onClick={}
>
CSV
</MenuItem>
<DropdownButton/>
I have a Login component in my header(bootstrap) which needs to open a Modal when I click. I am using the Modal from react-modal (npm package).
Even though I am not getting an error the popup won't open.
this.state={
openModal:false
}
closeModal=()=>{
this.setState({openModal:false})
}
handleOnClick=()=>{
this.setState({openModal:true}
}
Header Component
` <ul className="nav navbar-nav navbar-right">
<li onClick={this.handleOnClick()}><a href="#">
<Modal
isOpen={this.state.openModal}
onRequestClose={this.closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<span class="glyphicon glyphicon-user"></span></Modal> Sign Up</a></li>
<li><span class="glyphicon glyphicon-log-in"></span> Login</li>
</ul>`
You need to remove the brackets () from the call. What you give to onClick is the function to call, not the code to run like in other frameworks like Angular.
So, that'll be onClick={this.handleOnClick}
This is the working code.
<ul className="nav navbar-nav navbar-right">
<li onClick={this.handleOnClick}><a href="#">
<Modal
isOpen={this.state.openModal}
onRequestClose={this.closeModal}
style={customStyles}
content
><LoginComponent />
</Modal> Login</a></li>