What is the best way to modify the text color within a Navbar, especially when the Navbar is displayed within a separate component?

First question on StackOverflow.

I have a Next App and the Navbar is being imported in the _app.js file.

import Navbar from "../Components/Navbar";

function MyApp({ Component, pageProps }) {
  return (
    <>
      <Navbar />
      <Component {...pageProps} />
    </>
  );
}

The color of Navbar links is grey by default, which works fine on most pages with a light-colored background. However, on some pages, the color is not visible.

I am looking for a way to change the color of the Navbar links only when we are on a specific page. For example, when visiting the page http://localhost:3000/model3, the Navbar links should be white.

I am using the module approach for the Navbar and SASS as the CSS compiler with a Navbar.module.scss

Model3.js

import React from "react";
import section from "../styles/section.module.css";
import S from "../styles/models.module.css";
import E from "../styles/model3.module.css";
import Link from "next/link";
import Head from "next/head";
import { Fade } from "react-reveal";

function model3() {
  return (
    <div>
      <Head SameSite="None">
        <title>Model 3 | Tesla</title>
        <link
          rel="icon"
          href="https://www.tesla.com/themes/custom/tesla_frontend/assets/favicons/favicon.ico"
        />
      </Head>

      <div className={S.Wrapper}>
        <div className={S.Maincontainer}>
          <div className={S.Container}>
            <div className={E.TopSection}>
              <Fade bottom>
                <div className={section.carname}>
                  <div className={section.title}>
                    <h2 style={{ color: "white" }}>Model 3</h2>
                  </div>
                </div>
              </Fade>
              <Fade bottom>
                <div className={E.Details}>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>3.1 s</h3>
                    </div>
                    <div className={S.DetailsData}>0-60 mph*</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>358 mi</h3>
                    </div>
                    <div className={S.DetailsData}>Range (EPA est.)</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <div className={S.DetailsHeading}>
                      <h3>AWD</h3>
                    </div>
                    <div className={S.DetailsData}>Dual Motor</div>
                  </div>
                  <div className={S.DetailsContainer}>
                    <Link href="">
                      <span>Order Now</span>
                    </Link>
                  </div>
                </div>
              </Fade>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
export default model3;

If there is anything else I should include, please let me know. Thank you.

Please note that all Navlinks in the Navbar are currently grey. To improve visibility, I need to change them to white specifically on this page.

Here is how the page looks like.

Answer №1

There are numerous approaches you can take:

  • Utilize your route hooks - React Router offers useLocation(), where you can check the location and apply a class to change behavior according to the path.

  • Pass props to your navbar component.

  • Add a class to the body depending on the route, and then style your navbar using CSS .dark .navbar

The most suitable method will vary based on the layout of your project.

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

Exploring the realms of Django Administrator with external JavaScript integration

Currently, I am working with django 2.0 that includes jquery 2.2.3. My goal is to implement the ImageViewer Javascript app (https://github.com/s-yadav/ImageViewer) on one of my admin pages. I have added the necessary js and css files to the Media class wit ...

I'm having trouble building my Next.js application with Yarn Plug'n'Play in a monorepo, as it

My current setup involves running Next.js 13.0.5 using Yarn 3.2.1 and Lerna 5.6.1. The issue appears to be related to the build tool, as everything runs smoothly when I initiate the Next.js server itself with yarn dev. What is causing the error in my pro ...

Customizing TableRow Hover Effects in React with Material-UI

I've been spinning in circles with this issue. It seems like I just can't grasp how class overrides function in Material-ui. I attempted to follow the advice in this response: CSS on Material UI components However, it didn't produce any res ...

The use of .hidden in Tailwind-CSS does not result in the application of flex or block styles to the element

My intention is to hide the div on small screens and display it on larger screens starting from md: However, it seems like the .hidden class has a higher priority or may be interfering with some of my dependencies. <div className="hidden md:flex&qu ...

Can state values be utilized as content for Meta tags?

I am looking for a way to display image previews and titles when sharing a page link. In order to achieve this, I am using the Nextjs Head Component. The necessary details are fetched on page load and used as content for the meta attributes. let campaign = ...

Utilize the "rel" attribute within the text currently being shown through Colorbox

Can the rel text be utilized in the current text shown by Colorbox? I have a collection of images categorized with rel="Project Name" <a href="gallery/IN01 _GSM1 450x582.jpg" rel="Project Name" title="Global Supplier Management brochure Cover" name ...

Create a personalized compilation process that eliminates the two-way binding

In my Angular 1.5.8 application, I have created an attribute directive called my-directive. I am trying to apply this directive to an element while passing two additional parameters - one with one-way binding (@) and the other with two-way binding (=). Ev ...

The dynamic links in Knockout.js are unresponsive to clicks

I've been working on a new project that utilizes knockout js. The main issue I'm facing is with setting up a table to display images and information from a form, being stored in an observable array. Each image is wrapped in an anchor tag, and the ...

Utilize Angular to transform a JSON string into HTML text featuring organized bullet points

I am currently working on a project using Angular and I need to display some data from a JSON file on a webpage. The issue I am facing is that the message is quite lengthy, and I would like it to be presented in bulleted points. "messageToDisplay" ...

Issue with hasClass and addClass not working as expected

Why isn't the script below adding a class (.active) to .global-header when .navigation.primary has a class of .active? JS if($(".navigation.primary").hasClass("active")) { $('.global-header').addClass('active'); } HTML < ...

ERROR: No compatible version of jQuery Foundation could be located

Encountering issues while installing Foundation due to conflicts with Jquery. λ bower install foundation bower foundation#x cached https://github.com/zurb/bower-foundation.git#5.5.1 bower foundation#x validate 5.5.1 against https: ...

How can you easily center content vertically on a web page?

There has been plenty of back-and-forth regarding this issue, with proposed solutions ranging from pure CSS to pure HTML. Some can get quite complex, involving deeply nested divs and intricate CSS rules. I'm looking for a simple and uncomplicated answ ...

Navigating the process of transferring a function to a functional component within React

Hey there, I'm fairly new to using React and TypeScript and I am facing a small issue with moving a function from the file App.tsx to a functional component called WordAddingForm.tsx. Any help would be greatly appreciated! Let's start with my Ap ...

Linking the User Interface to the Controller and Database

My UI requires a text field where users can input the streamId (e.g. 1, 2, 3) and then click 'ok' to display a table on the screen from the database based on the streamId value. ...

After Effects Script: Apply various character styles from main text to subtext

I'm currently working on an After Effects script that can transfer the style and text of a text layer to another text layer in a different comp. The script works perfectly if the parent text layer only has one style, but I need it to handle a situatio ...

Using Angular to include a forward slash "/" in the text input for a date field

Hello everyone, I am a newcomer to AngularJS and I am looking to insert slashes in an input type text element. I prefer not to rely on external packages like angular-ui or input type Date. My goal is to have the format mm/dd/yyyy automatically applied as ...

Ways to expose an object in a Node module

One of my modules relies on a parsing functionality, with other modules needing to query the values provided by this parser. I have some key questions regarding its design: How should I approach building it in terms of design aspects? Which method should ...

Align the responsive image in the center of a container

I am facing a challenge in centering an image wrapped by an anchor tag inside a div. I have tried using display flexbox and justify-content:center for the image, which works to some extent. However, the height of the image remains constant while the width ...

Utilizing SetInterval for dynamically changing the CSS background image URL

I'm starting to feel frustrated with this situation... I have a solution where the CSS background is coming from a webservice. Currently, the system refreshes the entire page using HTML: <meta http-equiv="refresh" content="300" /> ... body { ...

Steps for executing ASP code pulled from the database

In my ASP project, I have a single page where clicking on different links retrieves corresponding HTML from the database and loads it. Some links are always present on the page (static HTML), while other divs display dynamic content fetched from the DB. Wi ...