Tips for getting rid of NavLink styling when Routes are not selected

I need assistance in restoring the default style of NavLink as shown in the screenshot below. The color should be white and without underlines. Here is my code snippet: Home is currently selected as the default path, and the activeClass property is functioning correctly.

https://i.sstatic.net/Uf8Xr.png

The code:

const NavBar = () => {
    return(
        <div className="navBar">
            <div className="logo">LOGO</div>
            <div className="navOptions">
                <ul>
                    <li>
                        <NavLink exact activeClassName="activeClass" to="/">Home</NavLink>
                    </li>
                    <li>
                        <NavLink exact activeClassName="activeClass" to="/advanceFilter">Advanced Search</NavLink>
                    </li>
                    <li>
                        <NavLink exact activeClassName="activeClass" to="viewAll">View All</NavLink>
                    </li>
                    <li>Logout</li>
                </ul>
            </div>
        </div>
    );
}

export default NavBar;

The CSS:

.navBar {
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 100%;
    color: white;
    font-weight: bold;
}

.logo {
    display: flex;
    flex: 1;
    align-items: center;
    padding-left: 50px;
}

.navOptions {
    display: flex;
    justify-content: flex-end;
    flex: 1;
    height: 100%;
}

//The li items don't have the color and text-decoration properties on them
li {
    display: inline;
    margin-right: 20px;
    cursor: pointer;
    height: 100%;
    text-decoration: none;
    color: white;
}

li:hover {
    background-color: gray;
}

ul {
    margin-right: 40px;
    list-style-type: none;
}

.activeClass {
    text-decoration: none;
    color: purple;
}

Answer №1

When using the NavLink component, make sure to update your CSS selector to target anchor tags that are children of li elements as well.

li, li a {
  display: inline;
  margin-right: 20px;
  cursor: pointer;
  height: 100%;
  text-decoration: none;
  color: white;
}

https://i.sstatic.net/pWpH0.png

Another option is to create a "navlink" class and define default non-active CSS styling there.

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

Tips on sending a chosen value as a parameter to the helper function

Is there a way to update the state based on the selected value, or how can I pass the selected value as an argument to the helper method? class SelectParker extends Component { state = { selectedValue: "", values: [], }; componentD ...

CSS KeyFrame animations

My goal is to have my elements gracefully fade in with 2 lines extending out of a circle, one to the left and one to the right. However, I am struggling to achieve this. Every time I attempt to display the lines, it ends up shrinking the entire design. M ...

Ways to dynamically load the material-ci-icons icon

Currently, I am using Material-ui@next and material-ui-next@next When using material-ui@next, only one icon can be loaded as shown below: import AddIcon from 'material-ui-icons/AddIcon'; function AddIconButton(props) { return ( <A ...

"Is it possible to establish a connection() from within the

I am faced with a challenge of creating React components using a dynamic list of properties. Below is an outline of my approach in mapStateToProps() and mapDispatchToProps(): export function mapStateToProps(id) { return function(state) { return sta ...

Using React's Context API to access context within a function and addressing the issue of undefined errors

I'm currently working on a project where I need to create a global variable that toggles between false and true as the user navigates between different views. My approach involves utilizing the Context API to establish this global variable. I have cre ...

Explore creating a dial number using React

Hey there, I'm currently working on developing a component. Just to clarify, I am not using react-native for this project. Instead, I would like to utilize React to scroll a number similar to how an image scrolls. https://i.sstatic.net/c1Tu8.png Th ...

Adjust the borderBottomColor of Material-UI TextField upon completion of input

When working with MUI to create a form, I noticed that the default TextField bottom border is grey, turns blue when focused, and then back to grey when focus is lost. My goal is to prevent it from losing the blue color after filling in the field: https:// ...

Reposition div when clicked

I have encountered a challenge where I am unable to perform a small task. My goal is to have the position of "div1" change upon clicking on "div2", taking into account that "div2" is nested inside "div1". Additionally, when clicking on "div2" again, "div1" ...

There was an error while trying to run the command '[node, -e, console.log(require('react-native/cli').bin);]' in ReactNative

I've been attempting to launch a project in react native, but I keep encountering the following error: :ReactNative:Running '[node, -e, console.log(require('react-native/cli').bin);]' command failed. FAILURE: Build failed with an ...

Tips for extracting a substring from a string in JavaScript or ReactJS

Is there a way to extract a specific string from a website URL in ReactJS? For example, if the URL is https://www.mrkashyap.com/users/manish, I only want to retrieve the word manish or anything after users/. I currently have the URL stored in a variable ...

Stagnant variable value after onClick event

After exploring various solutions, none seem to quite fit my needs. I want to update the variable "currentIndex" when a user clicks on an image. Currently, the change occurs within the onClick function but does not affect the outside variable. I am unsur ...

What is the best way to set up a proxy between Partytown and Gatsby during deployment on Netlify?

Trying to integrate GTM with Partytown has resulted in a CORS error for me. Does anyone know how to troubleshoot this issue? Here is the content of my gatsby-ssr.js file: import React from "react"; import { Partytown } from "@builder.io/par ...

Animate the toggling of classes in jQuery

Here is a code snippet that I came across: $('li input:checked').click(function() { $(this).parent().parent().toggleClass("uncheckedBoxBGColor", 1000); }); This code is functioning correctly when the element is clicked for the first time. I ...

The issue encountered with NextJS Link and next/script is that the Script fails to load upon navigation, yet it functions properly when using the <a> tag

I am currently developing an application that requires loading the "https://accounts.google.com/gsi/client" API. I utilized the Script component of NextJS, but when attempting to navigate using the Link from NextJS, the script fails to load again. However, ...

Exploring Next.js 13: Tailoring Next.js layouts to suit your route structure

In my latest project with Next.js version 13, I am encountering a situation involving a route called problems/[slug]. At the initial level of this route (e.g., problems/react), there is a sidebar and content section that are being shown using a custom layo ...

Tips for creating a validation section in a CRUD application using React.js

I have been working on developing a CRUD app using ReactJS. However, I am facing some challenges with the validation process as it does not seem to be working correctly and I am not receiving any results. As a beginner in this field, I would greatly apprec ...

Modifying pagination numbers with Reactjs: A step-by-step guide

I am currently working on Reactjs (nextjs) and I have successfully integrated the "Nextjs" framework. The pagination is working fine, but the buttons are displaying as "1,2,3,20" instead of "1,2...20" (showing all numbers without using "..."). How can I mo ...

verifying the user's screen dimensions

I'm currently exploring ways to customize the appearance of my website based on the viewer's screen resolution. I am interested in potentially optimizing the layout for vertical screens on mobile devices, and horizontal screens for laptops. Addit ...

What are some solutions for resolving the problem with the anchor attribute in Safari?

There is a problem with the anchor tag used in my website. The number 619-618-1660 is being displayed when I view the site on Safari browser or an iPhone, even though it is written as (href="tel:619-618-1660"). See the image at the link below for reference ...

Guide to resolving the issue of error Type 'void[] | undefined' cannot be assigned to type 'ReactNode'

I am attempting to map the data Array but I am encountering an error: Type 'void[] | undefined' is not assignable to type 'ReactNode'. Can someone please assist me in identifying what I am doing wrong here? Below is the code snippet: i ...