Search code examples
reactjsdrop-down-menureactstrap

Reactstrap dropdown opening multiples menu


Below is snapshot of my page:

enter image description here

I have used reactstrap dropdowns to bind the buttons with menu. Whenever I click one button all the dropdown menu are opening. Below snapshot is the dropdown issue:

enter image description here

Here is the code I have used:

 import React, { Component } from 'react';
    import './Home.css';
    import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';

    export class Home extends Component {
        constructor(props) {
            super(props);
            let $this = this;
            $this.toggle = $this.toggle.bind($this);
            $this.state =
                {
                    dropdownOpen: false
                };
        }
        toggle() {
            this.setState(prevState => ({
                dropdownOpen: !prevState.dropdownOpen
            }));
        }
    render() {
        return (
            <div className="table-div table-responsive-xl">
                <table className="table table-hover">
                    <thead>
                        <tr>
                            <th scope="col" />
                            <th scope="col">Authentication</th>
                        </tr>
                    </thead>
                    <tbody>
                        {this.state.slackmembers.map((item, key) => {
                            return (
                                <tr key={key}>
                                    <td scope="row" />

                                    <td>{item.Authentication}</td>
                                    <td>
                                        <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
                                            <DropdownToggle className="menu-button">
                                                <i className="fa fa-ellipsis-h" aria-hidden="true" type="ellipsis" />
                                            </DropdownToggle>
                                            <DropdownMenu>
                                                <DropdownItem style={{ fontWeight: 500, color: 'black' }}>First</DropdownItem>
                                                <DropdownItem style={{ fontWeight: 500, color: 'black' }}>Second</DropdownItem>
                                                <DropdownItem divider />
                                                <DropdownItem style={{ fontWeight: 500, color: 'red' }}>Last </DropdownItem>
                                            </DropdownMenu>
                                        </Dropdown>
                                    </td>
                                </tr>
                            );
                        })}                     
                    </tbody>
                </table>
            </div>
        );
    }

I don't know whats wrong in my approach. Can someone help on this issue.


Solution

  • You can create a new component for Dropdown and manage the toggle state like:

    default class TempDropdown extends React {
    
    constructor(props){
        super(props);
    
        this.state = {
            isOpen: false
        }
    }
    
    toggle = () => {
        this.setState({isOpen: !this.state.isOpen})
    }
    
    render(){
        return(
            <Dropdown isOpen={this.state.isOpen} toggle={this.toggle}>
                <DropdownToggle className="menu-button">
                    <i className="fa fa-ellipsis-h" aria-hidden="true" type="ellipsis" />
                </DropdownToggle>
                <DropdownMenu>
                    <DropdownItem style={{ fontWeight: 500, color: 'black' }}>First</DropdownItem>
                    <DropdownItem style={{ fontWeight: 500, color: 'black' }}>Second</DropdownItem>
                    <DropdownItem divider />
                    <DropdownItem style={{ fontWeight: 500, color: 'red' }}>Last </DropdownItem>
                </DropdownMenu>
            </Dropdown>
        )
    }
    

    }

    and then Use it in your Home Component and pass the data as props (if any) like:

    render() {
        return (
            <div className="table-div table-responsive-xl">
                <table className="table table-hover">
                    <thead>
                        <tr>
                            <th scope="col" />
                            <th scope="col">Authentication</th>
                        </tr>
                    </thead>
                    <tbody>
                        {this.state.slackmembers.map((item, key) => {
                            return (
                                <tr key={key}>
                                    <td scope="row" />
    
                                    <td>{item.Authentication}</td>
                                    <td>
                                        <TempDropDown />
                                    </td>
                                </tr>
                            );
                        })}                     
                    </tbody>
                </table>
            </div>
        );
    }
    

    Hope this will solves your issue :)