React.js onClick does not display the dropdown

Hello, I have a question regarding my navbar icon functionality. When I click on the icon, it is supposed to open a dropdown menu. However, although the div name changes when clicked, the CSS properties remain the same as the initial class. I am unsure where I am making a mistake and would appreciate any help. I thought it should have worked.

  <div className='action'>
        <div className='profile' onClick={this.menuToggle}>
          <img src={Avatar} alt="Avatar" />
        </div>

        <div className={`menuDropdown ${active ? "active" : ""}`}>
          <ul>
            <li><a href='#' />View Profile</li>
            <li><a href='#' />Logout</li>
          </ul>
        </div>
      </div>
    class NavBarClient extends Component {
  constructor() {
    super();
    this.state = {
      active: false

    };

  }



  // Function to toggle the menu
  menuToggle = () => {

    this.setState({ active: !this.state.active });

  }

  render() {
    const { active } = this.state;
.menuDropdown{
    position: absolute;
    top: 120px;
    right: -10px;
    padding: 10px 20px;
    background-color: #fff;
    width: 200px;
    box-sizing: 0 5px 25px rgba(0,0,0,0.1);
    border-radius: 15px;
    transition: 0.5s;
    visibility: hidden;
    opacity: 0;
    
    &.active{
        top:80px;
        visibility: visible;
        opacity: 1;
    }
}

Answer №1

If you want to modify the appearance of the menu dropdown in your CSS file, it is recommended to separate the .active CSS object from the .menuDropdown object.

.menuDropdown{
    /* Custom CSS properties for the menu dropdown */
    position: absolute;
    top: 120px;
    right: -10px;
    padding: 10px 20px;
    background-color: #fff;
    width: 200px;
    box-sizing: 0 5px 25px rgba(0,0,0,0.1);
    border-radius: 15px;
    transition: 0.5s;
    visibility: hidden;
    opacity: 0;
}

.active{
    /* Custom CSS properties for when the dropdown is active */
    top:80px;
    visibility: visible;
    opacity: 1;
}

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

When attempting to execute a script that includes document.write, it will not function as expected

Our web program is utilizing ajax and jquery, specifically Netsuite. I've been attempting to adjust elements on a page using document.ready and window.load methods in order to load an external script onto the page. Regardless of whether I load this ex ...

Is there a way to open an HTML file within the current Chrome app window?

Welcome, My goal is to create a Chrome App that serves as a replacement for the Chrome Dev Editor. Here is my current progress: background.js chrome.app.runtime.onLaunched.addListener(function() { chrome.app.window.create('backstage.html', { ...

Issue with Angular 7 Universal: components inside are failing to display

I have successfully implemented Angular 7 Universal with dynamic server-side rendering. However, I am facing an issue where dynamic components within the main component being rendered on the server are not rendered themselves. Here is an example of the re ...

The React state persists to reset back to its initial state consistently

Currently, I am developing a mobile signature feature for my website which involves three components: Form.js (the parent component using Formik for the form), Admin.js (a child of Form), and Signature.js (a child of Admin). Upon entering the Admin sectio ...

Error when using ReactDOM.render with React version 16.5.2: Node not found on component that has been unmounted

After updating my ReactJS project to the latest version using npm update in Visual Studio 2017, I encountered the following results: + @material-ui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234a404c4d5063110d130d10">[e ...

Changing the dropdown icon in Autocomplete with Material-UI: A Step-by-Step Guide

Is it possible to change the icon for the open/close dropdown list without affecting the functionality? I attempted to use endAdornment but lost functionality for both remove and open/close icons. I simply want to replace Material-UI's custom arrow w ...

Default Selection Issue with Radio Buttons in AngularJS

I am currently encountering an issue with the code snippet included in my directive template '<li ng-repeat="f in foos">' + '<input type="radio" ng-change="foo(f.key)" ng-model="selectedFoo" name="foos" id="{{f.key}}" value="{{f.ke ...

Unexpected JSON token error occurs in jQuery when valid input is provided

I encountered an error that I'm struggling to pinpoint. The issue seems to be related to the presence of the ' symbol in the JSON data. After thoroughly checking, I am positive that the PHP function json_encode is not responsible for adding this ...

Trouble with CSS positioned image rendering in Internet Explorer? Z-index not solving the issue?

I've been working on a timeline project where the completed sections should have a green background with a check mark image in the foreground. It appears correctly in Firefox, Chrome, and Safari. However, in IE 7 and 8 (and possibly 9), the layout is ...

Utilize ModifyDOM to erase text triggered by selecting a radio button

I have created a form that includes four radio buttons along with a reset button to clear the form. When a radio button is selected, information is displayed using "displayText". I am looking to add an onclick handler to trigger a function that will delete ...

Whenever I try to use Reactjs useState, an error always pops up

I am currently working with reactjs and utilizing the material-table to fetch data for an editable table. However, I have encountered an error similar to the image displayed. How can I resolve this issue? I have implemented useState for managing the edit ...

css boxShadow merger

Looking to create a sleek horizontal navigation bar using an unordered list with list items representing each element: To ensure the elements fit perfectly within the bar, I set the width of each at 25% and added a box-shadow for a border. Using a traditi ...

What could be causing the issue with "class not being recognized as a constructor" error that I am encountering?

When attempting to create an instance from modules in routing, I consistently encountered the error message "class is not a constructor." Below is the code snippet: export class Modules{ modules : object = { masterdata : MasterdataModule, shop : ...

Trouble accessing onclick function

My dataSend AJAX function is not being called when I use the onclick event. I have checked in the Inspector of my browser and confirmed that the click handler is attached to it. However, when I set a breakpoint in the function using the Debugger, it never ...

Setting up NestJs with TypeORM by utilizing environment files

In my setup, I have two different .env files named dev.env and staging.env. My database ORM is typeorm. I am seeking guidance on how to configure typeorm to read the appropriate config file whenever I launch the application. Currently, I am encountering ...

Utilizing Adjacent Sibling Selectors within the context of a specific enclosing class

I have a question regarding the following HTML structure: <div class="a"> <div class="b"></div> <div class="c"></div> </div> My goal is to apply a specific CSS rule only within the a class: .b + .c { margin-le ...

Executing an http.get request in Angular2 without using RxJS

Is there a method to retrieve data in Angular 2 without using Observable and Response dependencies within the service? I believe it's unnecessary for just one straightforward request. ...

Is it possible to pass multiple parameters in one onClick event?

My search bar is located below. Upon clicking the 'search' button, I wish for it to update results and redirect to a different page simultaneously. function SearchBar(props) {   const [innerSearch, setInnerSearch] = useState(""); ...

AngularJS directive error: Incorrect function invoked

I have two similar scenarios where I need to apply validators for specific files, even though I am aware that this goes against the DRY principle. However, being new to AngularJS, I am still learning the ropes. module.js var $moduleExample = angular.modu ...

What is the best way to pass an array as a parameter in Angular?

I have set up my routing module like this: const routes: Routes = [ { path: "engineering/:branches", component: BranchesTabsComponent }, { path: "humanities/:branches", component: BranchesTabsComponent }, ]; In the main-contin ...