Numerous dropdowns failing to display available options

As a React beginner, I am utilizing the Patternfly Dropdown component. However, I am encountering an issue when attempting to use multiple instances of the same dropdown on a single page. Upon clicking them, the options inside do not open as expected.

I am seeking guidance on how to modify the existing code to enable the usage of multiple dropdowns on the same page.

STATE:


    //dropdown menu
    this.onToggle = isOpen => {
      this.setState({
        isOpen
      });
    };
    this.onSelect = event => {
      this.setState({
        isOpen: !this.state.isOpen
      });
      this.onFocus();
    };
    this.onFocus = () => {
      const element = document.getElementById('toggle-id-1');
      element.focus();
    };
  

Under render()


    const dropdownItems = [
      <DropdownItem key="action1" component="button">
        1 Hour
      </DropdownItem>,
      <DropdownItem key="action2" component="button">
        1 Day
      </DropdownItem>,
      <DropdownItem key="action3" component="button">
        1 Week
      </DropdownItem>,
      <DropdownItem key="action4" component="button">
        Never
      </DropdownItem>,
     ];
  

Under Return


    <Dropdown
      onSelect={this.onSelect}
      toggle={
        <DropdownToggle id="toggle-id-1" onToggle={this.onToggle}>
          Dropdown
        </DropdownToggle>
      }
      isOpen={isOpen}
      dropdownItems={dropdownItems}
      autoFocus={false}
    />
    
    <Dropdown
      onSelect={this.onSelect}
      toggle={
        <DropdownToggle id="toggle-id-2" onToggle={this.onToggle}>
          Dropdown
        </DropdownToggle>
      }
      isOpen={isOpen}
      dropdownItems={dropdownItems}
      autoFocus={false}
    />
  

Answer №1

Make sure that you are not reusing state variables for both dropdown menus. To identify any potential errors, open your console and examine the output when interacting with the dropdowns.

If feasible, please share the error messages here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is window.getComputedStyle not functioning as anticipated?

Here is a function I created to determine the width and height of a text: function size_calculation(word,fontSize) { const div = document.body.appendChild(document.createElement('div')); div.textContent = word; div.style.cssText = ` fo ...

Conceal all elements until a search query is entered using the search bar

I want to create a search bar that hides all elements until the user actually searches for them, you can check out my JSfiddle for reference: `JSfiddle Link <div id="search"> <form> <input type="text" name="search" id="m ...

Attempting to embed Text components within another Text component, each with distinct font styles. Unfortunately, in one instance, the line height is causing certain text to be clipped

I am working on a React Native application and experimenting with creating styled text elements. I wanted to have different styles for each word in a sentence, so I nested Text components with different styles inside one another. For example: <T ...

Clicking on Fixed Positioning triggers a reset

When I activate the sidebar by clicking the menu button, it remains fixed in position until the first scroll. However, if I interact with the sidebar by clicking on it, the button resets and goes back to the top of the page. This issue seems to only occur ...

Styles for Material UI 5 class names

After upgrading from Mui 4 to 5, I am facing a challenge in using class names effectively. While the SX property allows me to apply styles to individual components, I am struggling with applying the same class to multiple components. In version 4, my code ...

Utilizing offsets/push in the correct manner

I have been developing a Bootstrap website and I wanted to get some feedback on my current code structure. The design is coming along nicely, but I am unsure if this aligns with best practices? <div class="container-fluid"> <div class="row"> ...

Is it possible to expand a div container dynamically when a button is pressed?

Currently, I am honing my skills in front-end web development by working with Angular 6 and the Materialize Framework. Although I am a beginner in this field, I find the use of cards in Materialize quite intriguing. One specific feature I'd like to im ...

Centered Layout using HTML and CSS

Exploring the world of Ruby on Rails and HTML has been quite an adventure. Today's question veers away from RoR and focuses on HTML and CSS. As I attempt to center my body, a peculiar issue arises: Help me solve this problem here How can I align the ...

Exploring the implementation of float type in TypeScript

Is it possible to use Number, or is there a more type-specific alternative? In the past, I have relied on Number and it has proven effective for me. For example, when defining a variable like percent:Number = 1.01... ...

What is the most straightforward method for implementing CSS transitions with fit-content for width?

Can you help me figure out how to smoothly transition the width of an element with the style attributes transition: 0.5s and width: fit-content? Specifically, I want to apply this transition to the entire element, not just the padding. Here is the Codepen ...

Tips for making animated SVGs come to life in a Next.js application

I'm attempting to animate an SVG in my upcoming app. Although the SVG is displayed on the site, it isn't animating. The animation was created with SVGator and requires CSS and JavaScript for functionality. I've made several attempts at imple ...

What is the best way to fill out border boxes in Bootstrap 4 for horizontal text alignment?

I'm struggling with this code snippet: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="row border border-success"> <div class="cols-12 col-sm-6 col-lg-4" class="b ...

What is the process for including attachments in Resend within NextJS?

I'm currently facing the challenge of figuring out how to send attachments using Resend in NextJS (API route or Server actions). I've been on the lookout for solutions but haven't found a working one yet. Despite trying various solutions fr ...

Is there a way to configure npm start to automatically launch in a specific web browser?

My PC defaults to using Firefox as the browser, but I want to open my react projects in Chrome. I attempted to change the script within package.json to "start": "BROWSER=chrome react-scripts start". I also experimented with placing ...

Having trouble getting Bootstrap CSS to work with ASP.net?

After using the bootstrap alerts class, I noticed that the view is not what I expected - https://i.sstatic.net/HqAFV.jpg Instead of that, I tried - https://i.sstatic.net/hooZy.jpg To fix this issue, I included the reference under the head tag like so: ...

Issue with Mui PaginationItem not syncing properly across multiple components

Hey there! I have set up Pagination and PaginationItem from MUI in my project. <Stack> <Pagination count={count} renderItem={(item) => ( <PaginationItem className="pagination" components={{ previ ...

Error 404: Unable to locate protobufjs version 5.0.0

My attempt to incorporate firebase into my project is met with an error: >npm install -S firebase npm ERR! code E404 npm ERR! 404 Not Found: protobufjs@^5.0.0 npm ERR! A complete log of this run can be found in: npm ERR! /Users/my_username/.npm/_l ...

When you click on the link in AngularJS, it will automatically open the textarea for you

I am aware of the function in CSS called "target" that changes the URL address. However, I need to achieve this effect without changing the URL. My main question is whether I can accomplish this using AngularJS. If so, could someone please explain how to a ...

A collection of items displayed in an unordered format across two columns

I need help creating a list that displays in two columns. One column for odd numbers, the other for even numbers. Here's an example of what I'm trying to achieve: +----------------------------------------------------+ | col- ...

Troubleshooting problems with file uploads on Multer in conjunction with a Node.js and MongoDB

I have developed an application using node js, react js, and mongodb. I am trying to implement file uploading functionality using multer, but I am facing issues with getting the files uploaded successfully. I have shared the code snippets related to both f ...