Conditional rendering of Materialize navbar links

I am currently working with React Materialize and React Router Dom. My goal is to display navlinks only to authenticated users using conditional rendering. However, when I implement it as shown below, the navlinks are displayed vertically instead of horizontally. Is there a workaround for this issue? Thank you

    <Navbar>
      {isAuthenticated && (
      <>
      <NavLink to="/locations" href="/locations">
        Locations
      </NavLink>
      <NavLink to="/categories" href="/categories">
        Categories
      </NavLink>
      </>
      )}
    </Navbar>

Answer №1

It appears that the Navbar component is rendering its children inside li elements. By wrapping them in a fragment, all NavLink elements are treated as one child element and placed within a single li.

To address this issue, here are two straightforward solutions:

  1. If there are only a few links, you can use conditional rendering:
<Navbar>
 {isAuthenticated && (<NavLink to="/locations" href="/locations">Locations</NavLink>)}
 {isAuthenticated && (<NavLink to="/categories" href="/categories">Categories</NavLink>)}
</Navbar>

However, this approach may not be ideal for a large number of links.

  1. Store the NavLink elements in an array and conditionally add authentication-dependent items:
// In the component:
const links = [/* some public links */];
const privateLinks = [
    // Make sure to include keys. The numbers used here are just examples.
    <NavLink key={1} to="/locations" href="/locations">Locations</NavLink>,
    <NavLink key={2} to="/categories" href="/categories">Categories</NavLink>
];

if(isAuthenticated){
 links.push(...privateLinks);
}
// In the template:
<Navbar>{links}</Navbar>

The logic involving the links arrays is simple (placing private links at the end) for demonstration purposes.

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

Guide to personalizing React Material-UI Next components

I'm currently utilizing Material UI's next branch for my project and I want to customize the style of the TableHead component within the table. My approach was to create a new component called MyTableHead, wrap it around the TableHead component, ...

Could you assist me in transforming this React star rating system?

I am seeking assistance with converting this code to change the rating number into a 5-star rating by dividing the rating by 2. The star's color should be maroon if the index of the star is less than Math.floor(rating / 2) - 1, otherwise it should be ...

How Bootstrap 4 Handles Input Resizing功能

Encountering an issue with an input element inside a Bootstrap card. Trying to make it behave like the button on the second card, but it keeps shrinking and remaining in-line. I've attempted to add some CSS properties like min-width or adjusting its w ...

Develop a unique splitter code that utilizes pure javascript and css, allowing users to drag and drop with precision

I am facing an issue with setting the cursor above my drop panel, as it results in a flicker effect. How can I set the cursor for my example to work properly? I have tried multiple different approaches to make this work. Although I am using the provided ...

Tips for increasing the font size using html and css?

I am encountering an issue with my HTML code: <div class="a"><font size="40"><font color="black">A</font></font></div> No matter what I try, I cannot seem to increase the font-size. Even adjusting the value in the co ...

Reactjs is experiencing issues with the data mapping function

Currently, I am developing with Reactjs and utilizing the nextjs framework. In my current project, I am working on fetching data from a specific URL (https://dummyjson.com/products) using the map function. However, I encountered an error message: TypeError ...

Building a Custom Admin Layout Component in React

I have been trying to create a custom layout for react-admin by following their documentation, but I ran into some issues. For example, the documentation mentioned using a theme in the component, which we didn't have. Additionally, there was a broken ...

Can an element in React be styled using the ID alone, without relying on className?

I'm in the process of transitioning an outdated website to React, and I'd prefer not to overhaul all the existing CSS files that have already been written. Many elements currently have their styles defined using an id. Is there a workaround to ma ...

Unexpected behavior of Bootstrap columns within nested rows, occurring between the small and extra-small breakpoints

Currently, I am in the process of developing a small website using Bootstrap 5. My goal is to have 6 elements displayed on two lines for breakpoints greater than or equal to md and on a single line for smaller breakpoints. It all seems to be working fine f ...

I am struggling to understand why the cards in my tremor project using next.js are not refreshing the frontend display

I am currently working on developing my Next.js application with Tremor. However, when I view my page on localhost, it doesn't seem to be rendering as I expected. Below is my page.tsx file: 'use client' import { Card, Text, Subtitle } f ...

Changing the position from fixed to static

It's strange, but for some reason when I attempt to apply position:fixed using jQuery: $('#footer').css('cssText', 'position:fixed !important;'); to my footer, the result on the page is different: <div id="footer" ...

Unable to input data into form field using react-hook-form and react-places-autocomplete

I came across an issue while using the react-places-autocomplete library in conjunction with react-hook-form. The input field I am using is a MUI TextField component. I am registering the value using the 'register' function provided by react-hook ...

What is the best way to cut out a portion of a div using CSS?

I have a scaled down version of my project, but it's not quite what I need. I would like the green div to have some transparency so that the content behind both divs is visible (there is none in the example, but there is in my actual project). However ...

Constructing a table using an array in React

I need assistance with creating a table based on salary data for different sectors. I have an array with example data that includes currencies and sectors. const data=[ ['Euro','Tech'], ['USD','Tech'], ['GBX&apo ...

What is the best way to ensure that an iframe adjusts its height to fit the content within a tabbed container?

Is there a way to dynamically set the height of an iframe to match the content inside it when clicking on a tabbed plane? Initially, I used a fixed height of 1000px. How can this be achieved? <div class="container-fluid text-center"> <div ...

Discover the countdown to an upcoming event with MomentJS

I am in the process of calculating the remaining time until an event begins. For instance, if my event is set for tomorrow at 7:30 pm, I aim to display the number of hours and minutes left before the event commences. My current approach involves checking ...

Safe method to pass arguments in React Router

Route path="id/:value" component={componentName} /> Do you believe this is an effective approach for passing parameters in React.js? ...

Use ngFor to align items to the left in your design

I am working on a div element where I need to dynamically add inputs that should form a number with a specific format. To ensure the inputs start from the left, I have applied the justify-content-end class. HTML: <div class='row justify-content-end ...

The Swig template displays the output tag prior to processing the content

Currently, I am developing an isomorphic React/Flux application and utilizing Swig for rendering the content. However, upon opening the site, a blank page briefly appears with the placeholder {{ html|safe }} output tag before displaying the actual content ...

ReactAppI am facing an issue where the browser fails to refresh the page upon saving changes in my JavaScript file using the VS code editor

Can anyone remind me of the tool we can install to refresh the browser page automatically whenever we save changes in our editor? ...