CSS struggles after implementing conditional checks in return statement for an unordered list

I'm encountering a CSS issue with the header section.

When I include the following code snippet in my code to display a tab based on a condition, the entire header list doesn't appear in a single horizontal line:

<div>
    {isAdmin(user) ?(
        <div >
            <li>
                <NavLink to="/datarequest">Data Request</NavLink>
            </li>
            <li>
                <NavLink to="/datarequestperson">Person Data Request</NavLink>
            </li>
        </div>
                        
    ) : (
        <li>
            <NavLink to="/datarequest">Data Request</NavLink>
        </li>

    )
    }
</div>

It works fine when I remove the above part and keep it like this, which is not ideal as I need to hide a tab based on a certain condition:

<li>
    <NavLink to="/datarequest">Data Request</NavLink>
</li>
<li>
    <NavLink to="/datarequestperson">Person Data Request</NavLink>
</li>               

            

I found the CSS related to the header-list class in the code:

ul.header-list li {
    display: inline;
    list-style-type: none;
    margin: 0;
}
ul.header-list {
    background-color: #fff;
    padding: 0 15px 0 0;
    float: right;
    margin: 0px;
}
ul.header-list li a {
    color: #000;
    text-decoration: none;
    padding: 20px 12px;
    display: inline-block;
    transition: 0.3s ease-in-out;
    -webkit-transition: 0.3s ease-in-out;
    -moz-transition: 0.3s ease-in-out;
    margin: 0 2px;        
}

ul.header-list li a.active, ul.header-list li a:hover{
    background: #0f5a25;
    color: #fff;
    transition: 0.3s ease-in-out;
    -webkit-transition: 0.3s ease-in-out;
    -moz-transition: 0.3s ease-in-out; 
    cursor: pointer;       
}

How can I resolve this issue so that I can also display the tabs based on the isAdmin condition without affecting the CSS?

The return method in my code looks like this:

return (
    
    <div>
    {
        this.state.isAuthenticated ? (
            <BrowserRouter basename={process.env.REACT_APP_ROUTER_BASE || ''}>
                <div>
                    <div className="header">
                        <div className="header-logo">
                        <img src="images/mylogo.jpg"/>
                        <span>DATA GRID</span>
                        </div>

                        <div className="logout_sec">
                        <div className="logout_icon">
                            <a href="javascript:void(0)" onClick={this.logoutDialogOpen}> <FontAwesomeIcon
                                style={{fontSize: '1.5em'}} icon={faSignOutAlt}/></a>
                        </div>
                        </div>                                

                        <ul className="header-list">
                        <li>
                            <NavLink exact to="/">
                                Home
                            </NavLink>
                        </li>
                        <li>
                            <NavLink to="/myprojects">My Projects</NavLink>
                        </li>
                        ... (remaining HTML code goes here)
                    </ul>

                        {/* Popup COde start */}
                        {logoutDialog}
                        {/* Popup code End */}
                    </div>

                    <div id="forNotification"></div>
                    <div>
                        <Switch>
                            <Route exact path="/" component={Home}/>
                            ... (more route declarations)
                            <Redirect to="/404"/>
                        </Switch>
                    </div>
                </div>
            </BrowserRouter>
        ) : null
    }
    </div>
);

Answer №1

If you want to display multiple elements without any extra wrapping element after rendering, you can achieve this by using Fragment, <>, or [ele1, ele2]. Take a look at the example below:

{isAdmin(user) ? (
  <Fragment>
     <li>
       <NavLink to="/datarequest">Data Request</NavLink>
     </li>
     <li>
        <NavLink to="/datarequestperson">Person Data Request</NavLink>
     </li>
   </Fragment>
  ) : (
     <li>
        <NavLink to="/datarequest">Data Request</NavLink>
     </li>

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

What is the best way to transfer information between two React.js files?

I am currently finding it inefficient to pass data from App.js to another .js file within React by constantly reading and writing from local storage. I would prefer to only retrieve data from local storage once when the App.js component mounts. App.js: ...

Enhancing User Interfaces with React and CSS Styling

I need to incorporate two unique React components on my webpage: Component1 and Component2. Code for Page1: <Component1/> <Component2/> While the individual components will have their own CSS files for styling, I also have a page1.css file f ...

Error: Unable to convert Mongoose object to ObjectId

Currently tackling an issue with my small blog app and I seem to be stuck at this error- { "message": "Cast to ObjectId failed for value \" 597c4ce202ca9c353fc80e8a\" at path \"_id\" for model \"Blog\"", "name": "CastErr ...

What is the reason behind Angular's continued use of JSON for encoding requests? (specifically in $http and $httpParamSerializerJ

Is there a way to set Angular to automatically send requests as x-www-form-urlencoded instead of JSON by default? Angular version 1.4.5 I've tried the following code snippet but it doesn't seem to work: angular.module('APP').config( ...

Is it possible to enable auto-completion for IDs in a separate CSS file using WebStorm 7?

I am new to using WebStorm 7 and currently working on building a simple HTML/CSS website. Initially, I included my CSS within the HTML file using the style tag, but now I have decided to move it to a separate file. Auto completion is functioning for all h ...

Component wrapped in wrapper not displaying

I have been attempting to conditionally render a component using a wrapper function. However, I am facing an issue where the component does not render when the condition is met. App.js import { BrowserRouter, Routes, Route } from "react-router-dom&quo ...

Is using float:right making the jquery slide toggle dropdown div (triggered by hover) appear glitchy?

Utilizing jQuery's slidetoggle and hover functions, I have successfully implemented a dropdown feature in my div. Within this div, there is various information displayed including the date, a note counter, and three buttons. The first two are Tumblr ...

Add the file to the current directory

As a newer Angular developer, I am embarking on the task of creating a web page that enables users to upload files, with the intention of storing them in a specific folder within the working directory. The current location of the upload page component is ...

Leveraging jQuery to extract the value from a concealed form field

Seeking assistance with a jQuery issue... I am attempting to use jQuery to retrieve the values of hidden fields in a form. The problem I am facing is that there are multiple forms displayed on the same page (result set items for updating), and the jQuery ...

Divide the output results into two equal horizontal sections within the same HTML table

Is there a way to dynamically split the output of a specific result into two horizontal sections that fit within the browser width? The number of rows returned from the service is variable, so a fixed solution won't work. I also want to avoid manually ...

Tips for replacing global functions during unit testing using Jest

Currently, I am in the process of creating unit tests for a module and could use some assistance with managing global variables and functions. Allow me to explain my query below: Imagine the module I wish to test is named 'needTest.js' and is st ...

Guide to Implementing i18n-iso-countries Library in React

I am currently developing a React application and attempting to utilize the i18n-iso-countries package to retrieve a countries object in English where keys represent iso codes and values represent country names. This process is straightforward in Node.js, ...

Animated collapse with margin effect in Material-UI

I have been utilizing the MUI-Collapse component to display collapsible content within a list. I am looking to add vertical spacing between the Collapse components in the list, but I do not want the spacing to remain when the Collapse is collapsed. I am ha ...

Struggling with aligning content within a div using CSS grid techniques

I am experimenting with CSS Grid and running into issues with overlaying content. The structure consists of a top-level container defined as a CSS grid (class="container"), followed by a content grid (class="content") that divides into 3 rows (header, labe ...

What is the best approach to updating multiple rows instead of just the first row using the update button located on both sides using PHP and AJAX?

Starting fresh, so I might sound like a beginner with this question. How can I make use of the update button to update specific rows instead of just the top row? Every time I try to update, it only works for the top row. Here's the code snippet from ...

What are the steps for configuring clusters in an expressjs 4.x application?

I currently have an expressjs generated app that is configured with socket io and I want to incorporate nodejs clusters into it. However, the challenge lies in the fact that in Express 4.x, the server listening configuration now resides in the bin/www file ...

Utilizing distinct JavaScript, JQuery, or CSS for individual Controllers within the Codeigniter framework

Currently, I am involved in a Codeigniter v3 project where we are developing a comprehensive application for a large organization. To optimize the loading speed of each page, I am looking to integrate custom JQuery and CSS files/code specific to each Con ...

Converting React JSX Material UI components into a string: A comprehensive guide

My challenge is to include JSX in a string as I'm developing a dynamic multi-tab strip. The number of tabs can vary from 1 to 4. The issue arises with using JSX because it requires elements to be closed, but in this scenario, the closing tags need to ...

What could be causing the .env.development file to malfunction in my Next.js application?

I am currently working on Jest and testing library tests. Let's discuss a component named BenchmarksPage. Please pay attention to the first line in its return statement. import { Box, capitalize, Container, FormControl, InputLabel, MenuI ...

The Art of Revealing an Element

Is it possible to manipulate a parent div element in JavaScript without affecting its child nodes? For example, transforming this structure: <div class="parent"> <div class="child"> <div class="grandchil ...