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

Is there a way to automate the distribution of tasks to users in order to ensure that each user receives an equal number of assignments?

I'm in the process of developing an automated task manager that assigns tasks to users based on their role. Currently, I'm randomly selecting a user with the same role as the task from the list of users and assigning the task to them using math.r ...

Component loader with dynamic rendering for multiple components

Currently, I am in search of a method to dynamically render components within my Angular application. While exploring various options, I came across the concept of dynamic component loading in Angular (refer to https://angular.io/guide/dynamic-component-lo ...

Immersive jQuery slideshow embellished with an interactive counter, captivating thumbnails, dynamic progress bar,

Hey there! I'm currently working on my very first website and I could really use some assistance in creating a slider with images. I've tried searching for a solution to my problem online, but even after attempting to fix the suggested plugin, I ...

Next-redux-wrapper displays a Typescript warning if the state is not present or cannot be assigned to its specified types

I am currently using next-redux-wrapper and encountering two type errors. Has anyone else experienced this issue before, and if so, how did you resolve it? Despite the errors, the code is functional, and I can view my state in the redux-devtools-extensio ...

Can you explain the significance of the '#' symbol within the input tag?

I was reading an article about Angular 2 and came across a code snippet that uses <input type='text' #hobby>. This "#" symbol is being used to extract the value typed into the textbox without using ngModal. I am confused about what exactly ...

What is the best way to send extra parameters to an ajax callback function?

Currently, I am implementing an ajax call in the following manner: util.AjaxCall(url, successCallbackFunction, errorCallbackFunction); function successCallbackFunction(result) { // Result returned from Ajax } Although everything is functioning correc ...

Run PHP code using JavaScript

I am currently using Windows and attempting to use JavaScript/PHP to call a specific form that is saved in a different location. The file D:\Test\Form.php has the following content: <form action="D:\Test\submit.php" method="post"&g ...

Guide on using axios to send form values dynamically as they change in a select dropdown?

I have successfully populated a select form from MaterialUI with choices received through an axios GET request. But now, in order to proceed, I need to make an axios POST request when the user selects an option in my handleChange(event) function. The data ...

Ways to ensure a function is only triggered once using onmouseover

I'm fairly new to JavaScript and I've been attempting to create a function that only runs once. Here's the logo I've been trying to animate: <img src="img/logot2.png" id="logoutjs" onmouseover="move()" ...

"Customize the appearance of a List Element in Material UI by changing

How can I modify the color of a specific material ui list item? <ListItemText primary={mspvar} secondary="buy"> ...

What is the best way to determine the size of CSS elements relative to other elements?

Is there a way to calculate the size of an element by using calc() or any other method based on the size of another DOM element? ...

Leveraging an external script for enhanced functionality in React/Meteor application

I'm currently facing a challenge incorporating an external script into my React component within Meteor. I've experimented with directly placing the script tag in my component as follows: TheLounge = React.createClass({ render() { return ( ...

What steps can I take to enhance the efficiency of this JavaScript DOM data manipulation algorithm?

Purpose I am working with a DOM that contains around 70 elements (divs with content). I have the need to move and toggle the display of these divs frequently and rapidly. Speed is crucial in this process. The trigger for moving and toggling these divs is ...

Use Jquery to retrieve the innerhtml content

Hey there! I'm currently in the process of learning Jquery and have encountered an issue with accessing form elements. My script is designed to open a div and then fill it with a predefined HTML form. To achieve this, I'm using ajax to fetch the ...

Utilizing Javascript to share images from a public Facebook page to a user's timeline via the Facebook API

Can someone assist me with finding a way to share a photo from a public Facebook page to the user's timeline using JavaScript? Is it possible to achieve this with FB.api or FB.ui? I have successfully shared feeds to the timeline using FB.ui, but am no ...

Implementing Angular CDK for a dynamic drag-and-drop list featuring both parent and child elements

I'm currently working on setting up a drag and drop list within Angular using the Angular CDK library. My goal is to create a list that includes both parent and child elements, with the ability to drag and drop both parent items as well as their respe ...

What is the best way to determine if any of the objects in an array contain a "completed" property with a value of false using JavaScript and React?

Is there a way to determine if at least one item in an array of objects has a completed property with a value of false using JavaScript and React? Here is an example array of objects: const items = [ { id: "32", jobs: [ ...

Prevent Scrolling of Body Content within a Specified Div

In my current situation, I am dealing with a large number of divs (over 300) that are being used as part of an interactive background. The issue arises when viewing the page on mobile versus desktop – there are too many divs on desktop, causing excessive ...

What is the best way to pinpoint particular text within a paragraph that includes numerous line breaks?

So here is the puzzling situation I'm grappling with. Here's a peek at the HTML snippet: <p>This paragraph has <br><br> two unusual line breaks <br><br> and it happens TWICE!</p> The problem arises when tryi ...