Using props as classnames in next.js applications

I am currently attempting to create a dynamic header for each page of my app that changes color based on the current page. Here is my approach:

<Header className="headerBitcoin"></Header>

My goal is to have the same header component displayed on all 4 pages, with the ability to simply change the className to alter the background color without affecting anything else.
Below is the code for the header component:

import styles from "../../styles/Home.module.css";
export default function Header(props) {
  return (
    <div >
      <div className={props.className}>aaaaa</div>
      <div className={styles.row}>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
      </div>{" "}
    </div>
  );
}

Currently, the styling for the tabs and row is working as expected, but the header is not reflecting its assigned style.
Upon inspecting the console, I noticed that the header is receiving the className headerBitcoin, while the row below it is assigned the className "Home_row__88lPM".
This is my first time using next.js, and I suspect there may be an error in my implementation as this functionality typically works in React. Any assistance would be greatly appreciated.

Answer №1

Avoid the following practice:

<div className={props.className}>aaaaa</div>

Consider using this alternative method instead:

<div className={styles[props.className]}>aaaaa</div>

In my opinion, this approach should be effective.

Answer №2

It seems that the styles are not being applied because the headerBitcoin styles are defined in your CSS module.

If you want to apply a class in this manner (

className="headerBitcoin"
), you should define the class in your global CSS file instead.

If you intended to use the headerBitcoin class defined in Home.module.css, then you will need to change the className to utilize the scoped styles.

import styles from "../../styles/Home.module.css";

export default function Header(props) {
  return (
    <div >
      <div className={styles[props.className]}>aaaaa</div>
      // ...
    </div>
  );
}

Answer №3

If you want to assign props value to className, here's one way to do it

import styles from "../../styles/Home.module.css";

export default function Header(props) {

  const customClass = 'custom-class'

  return (
    <div >
      <div className={styles[`${customClass}` + props]}>aaaaa</div>
      // ...
    </div>
  );
}

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 could be causing the issue with my custom AlloyEditor UI extension?

After following the instructions in this guide to integrate alloyeditor as a WYSIWYG editor into Contentful, I successfully added the extension to my contentful staging space. However, despite copying the html page from the github repository and includin ...

Setting a backgroundImage URL using props in React is a useful feature that allows for

Trying to create a React component that changes its background image based on the props received. Any suggestions on how to achieve this functionality? The props object includes a property called synmain, which is a string linking to an image "../image.p ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

Updating a component from a different source

As a newcomer to React, I'm curious about the ability to update one component's content based on events from another component. I have two React components set up. The first component loads data when the page initializes, while the second compon ...

What is the best way to eliminate whitespaces and newlines when using "document.execCommand("copy")" function?

I'm currently working on a code that allows users to copy highlighted text without using the keyboard or right-clicking. I also need to remove any extra spaces or line breaks using regex after the text is selected. However, I am facing some issues wit ...

Navigating from a web address to making an asynchronous request using history.js

As I work on a small website that features multiple pages with similar layouts, I often find that only the content within a specific div varies. The rest of the elements such as navigation and header remain consistent throughout. To address this, I have s ...

From creating a simple jQuery fiddle, let's delve into the world

Here is a code snippet I'm trying to transition from jQuery to an Angular directive. Visit this link to view the original code: http://jsfiddle.net/rhtr1w04/ Below is my implementation in app.js: angular.module('app',[]).directive('an ...

Ways to address the dependencies problems when using npm install @types/react?

Encountering an issue with the @types/react package. This pertains to a small UI project developed on top of the nextron platform. Everything was functioning smoothly until I had to reinstall my Windows OS. Attempting to rebuild the project using yarn an ...

Leveraging the power of LocalNotificationSchedule and Firebase Notification within a single React Native application

Can localNotificationSchedule and Firebase Remote Push Notification be used simultaneously in a React Native app? Utilizing the shared library on https://github.com/zo0r/react-native-push-notification indicates that the AndroidManifest.xml requires two sep ...

The UseEffect function ceases to function properly upon refreshing the website

I'm currently using ReactJS for a project. I have a form that is intended to serve as the configuration for another form. The structure of this specific form is as follows: const [startingDate, setStartingDate] = useState(); const [endingDate, set ...

How can you utilize data captured through a JavaScript onchange event in conjunction with another event?

Is there a way to utilize the firmType data captured during event 1 in event 2? code event 1 $("body").on("change","#so_customer", function(v){ customerFirm = $(this).find(":selected").data("employer"); $("#customer_employer").val(cust ...

Is there a delay in using ngShow and ngClick for authentication in AngularJS and Firebase?

Just getting started with AngularJS and encountering an unusual issue with Firebase authentication. The basic setup displays the current user status (logged in or not) along with options to sign in and out. Oddly, when I click the Log-in button for the fi ...

Create a path on the Google Map that follows the designated route

I am looking for a solution similar to one found here: Sample However, I have been unable to find a suitable solution anywhere. The main issue is being able to follow the route in order to draw a line between two points. Can anyone provide guidance on ho ...

The animation in my SVG comes to a halt at a certain point

I am having an issue with my SVG element (shown below) where it stops animating after a certain period of time. I want the animation to continue indefinitely like in this jsfiddle. .path { stroke-dasharray: 20; animation: dash 10s linear; } @ ...

Error: Unable to access the 'name' property of an undefined variable

When running the code, I encountered a "TypeError: Cannot read property 'name' of undefined" error, even though when I console.log it, it provides me with the object import React from "react"; class Random extends React.Component { constructo ...

Error in Javascript chrome when trying to determine the length of an array

I am facing an unusual issue with the JavaScript console in Chrome. When I type the following code into the console: var numbers = new Array(["/php/.svn/tmp", "/php/.svn/props"]); it returns "undefined." This leads me to believe that 'numbers' ...

Changing the format of PHP SQL results from vertical to horizontal

Is there a way to convert the vertical table generated by the following code into a horizontal one with stylish formatting? Additionally, is it possible to display certain columns in a popup window based on query parameters using jQuery or CSS? <html&g ...

Encountering a TypeError while trying to import grapesjs into a nextjs project, specifically receiving the error: "Cannot read properties of null (reading 'querySelector')

I encountered an issue while trying to integrate grapesjs into a nextjs project. The error I received was TypeError: Cannot read properties of null (reading 'querySelector') It appears that grapesjs is looking for the "#gjs" container by its id ...

Trigger animation once you've scrolled past a designated point in the document

I created a count-up counter animation using JavaScript, but the issue is that the counter starts animating as soon as I refresh the page regardless of where I am on the page or if the counter is even visible. I would like the counter to only start workin ...

Challenges with implementing @Html.EditorForModel

Currently, I am developing a web application using asp.net's mvc-5 framework. In this project, I have implemented the following model class: public class Details4 { [HiddenInput(DisplayValue=false)] public string RESOURCENAME { se ...