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

The react-datepicker component is unable to set the state to the format dd/MM/yy

The date is currently shown in the correct format (31/08/21), but when onChange gets triggered, startDate changes to something like this: Tue Aug 31 2021 21:29:17 GMT+0200 (Central European Summer Time) Is there a way to maintain the display format I wa ...

Using XSLT with JavaScript

I am currently working with a set of XML files that are linked to XSLT files for rendering as HTML on a web browser. Some of these XML files contain links that would typically trigger an AJAX call to fetch HTML and insert it into a specific DIV element on ...

Updating div with specific class based on selected values

I have utilized Angular to display content based on selected values from a dropdown menu. Now, I am seeking a solution to dynamically add a CSS class to a div when specific values are chosen. In other words, I want to apply certain styling to the div base ...

Tips for effectively handling a navigation bar in nextjs version 14

In my project using nextjs version 14, I wanted to create a Navbar component and incorporate it into the layout of the root /app folder. After creating the component and adding it to the layout, I encountered a major issue: The component only renders the f ...

Learn the process of converting Null values to empty strings within a chain of functions when manipulating a database

Currently, I am utilizing the lodash library and have the following code in place: data: _(responseData.data) .pick(['title', 'layout', 'slug', 'author', 'seo', 'css', 'js']) ...

Incorporate functionality into a button using jQuery

I am working on a table that displays data in different levels (Parent, Child, Grandson). When I click on the parent row, it expands to show new rows related to the child level. Similarly, clicking on a child row reveals a third level known as the grandson ...

The Angular component is failing to display the template

After following a tutorial on UI-Router () I have implemented the following states in my Angular application: angular .module('appRoutes', ["ui.router"]) .config(['$stateProvider', '$urlRouterProvider', function($sta ...

Error message "Unable to access property 'rotation' of an object that does not exist - Three.js"

I'm currently facing an issue where my code is executing as expected, but there are two errors popping up in the console saying "Cannot read property 'rotation' of undefined". It's puzzling because both variables are defined globally. I ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...

implement a new directive in AngularJS that references another directive in HTML

Check out the Fiddle here Upon button click, a modal window appears. Inside this modal, there is a <template-placeholder></template-placeholder>. When the button is clicked, an if-check is performed to ensure it's the correct button. If ...

:before pseudo-element causing interference with child elements

Struggling to understand how an absolutely positioned :before element works. I need a large quote mark to be placed behind a testimonial. I opted for a :before element because it is a font, making it scalable for retina displays and saving an extra HTTP re ...

Using jQuery to set the background-image on the body's after pseudo-element with CSS

I am currently utilizing body:after for setting the page wallpaper. body:after { background-image: url('assets/img/wallpapers/<?php echo $getWallpaperFile; ?>'); } CSS content: ' '; display: block; position: absolute; left: ...

Adding the text-success class to a Bootstrap 5 table row with zebra striping does not produce any noticeable change

I am encountering an issue with a Bootstrap 5 table that has the class table-striped. When a user clicks on a row, I have implemented some jQuery code to toggle the class text-success on the row for highlighting/unhighlighting purposes. The highlighting f ...

Encountering Server Error 500 while trying to deploy NodeJS Express application on GCP App Engine

My goal is to deploy a NodeJS app on gcloud that hosts my express api. Whenever I run npm start locally, I receive the following message: >npm start > [email protected] start E:\My_Project\My_API > node index.js Running API on por ...

Retrieve information and functions from one component in a separate component

I currently have two components: ContainerSidebar.vue <!-- Sidebar --> <div id="b-sidebar"> <div class="change-image"> <img :src="profile.avatar != null ? profile.avatar+'#&apo ...

Using Nextjs Image as the Background for Layout

I have a question regarding implementing a common background image for all pages in my app. Currently, I have the main page.js code that displays an image as the background using Next Image component. However, I would like this background to be present thr ...

Discover Vue3's efficient event handling feature that allows you to easily listen to events from dynamically generated child components

I am dynamically creating a Vue component and need to listen to the event it emits. While I know that you can use @eventName in the markup, my component is being created using createApp. const div = document.createElement('div'); this.$refs.logi ...

Error message encountered when submitting a form after receiving an AJAX response: VerifyCsrfToken Exception

I'm encountering an issue with my AJAX functionality that involves rendering a form with multiple input fields and a submit button. Here is the AJAX call: <script type="text/javascript"> $('#call_filter').click(function() { $.aja ...

How can CakePhp's $ajax->link be used to manipulate the result on the complete action?

Recently, I've started working with cakePhp to handle ajax requests using prototype. While my controller is returning the correct value, I'm struggling to figure out how to properly handle it when it comes back looking like this: <?php echo ...

Overlap between Material UI OutlinedInput and InputLabel components present an issue

What is the solution to the overlap problem between InputLabel and OutlinedInput in Material UI? Visit codesandbox for demo <FormControl> <InputLabel htmlFor="my-input">Email address</InputLabel> <OutlinedInput id=&q ...