What is the best way to send props to a CSS module in React?

Currently utilizing NextJS, I have a route named about that is displayed through page.js. Within /about/page.js, I am incorporating a component called <Hero /> and aiming to pass a prop to

<Hero heroHeight={'height: 400px'} />
to my CSS module to aid in styling.

Hero.js

'use client';

import styles from '../page.module.css';
import style from './Hero.module.css';

const Hero = ({ heroHeight }) => {
  return (
    <div className={`${styles.pageWidth} ${style[heroHeight.updateTheHeight]`}>

    </div>
  );
};

export default Hero;

Hero.module.css

.updateTheHeight{
  border: 1px solid green;
  /* Aim to pass the prop 'height: 400px' here */
}

The current code is not functioning as intended. I aim to pass the prop value of height: 400px to the .updateTheHeight class. Can anyone assist in identifying the issue in my code? Any guidance is highly appreciated. Thank you!

Answer №1

It seems the issue with the code lies in attempting to combine CSS modules with dynamic styles, which is not the intended use of CSS modules. CSS modules are better suited for static, predefined class names. However, there is a straightforward solution to this problem.

To incorporate dynamic styles, you can make use of inline styles. Inline styles allow you to directly insert JavaScript values into the style attribute.

One way to address this is by modifying your Hero.js file. Instead of trying to apply your prop to a CSS module class, you can directly include it in a style object. Consider the following example:

const Hero = ({ heroHeight }) => {
 const dynamicStyle = {
 height: heroHeight, // Insert dynamic height here
 border: '1px solid green', // Assuming a consistent green border is desired
};

 return (
  <div className={styles.pageWidth} style={dynamicStyle}>
   {/* Your Hero content */}
  </div>
 );
};

When utilizing your Hero component, simply pass the height value as before:

<Hero heroHeight="400px" />

Answer №2

It's not possible to directly pass values to CSS files. However, there are alternative methods to achieve this.

One approach is to utilize the style attribute to dynamically set the height.

'use client';

import styles from '../page.module.css';
import style from './Hero.module.css';

const Hero = ({ heroHeight }) => {
 return (
   <div className={`${styles.pageWidth} ${style[heroHeight.updateTheHeight]`} style={heroHeight}>

   </div>
 );
};

export default Hero;

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

Database storing incorrect date values

After successfully displaying the current year and month in a textbox, an issue arises when submitting the data to the database. Instead of the expected value from the textbox, it defaults to 2014. What could be causing this discrepancy? function YearM ...

In a React Redux application, I struggle with typing in the input fields and making changes

Can someone help me with a problem I'm having with this code? When I try to type on the inputs, nothing happens. Here is the link to the code: https://stackblitz.com/edit/react-hghsyk?file=reducers%2Findex.js ...

What is the method by which the asynchronous function produces the ultimate output?

Is there a way to modify a Dojo framework-created class with an asynchronous method so that it only returns the final value instead of a Promise or any other type? ...

Incorporating a client certificate into ReactJS requests for an external REST API that mandates mutual TLS authentication

I am currently learning about reactJS and I am working on integrating mutual TLS into a web application that is developed using reactJS and the node.js framework. As part of this project, I need to access an external REST API that requires a client certifi ...

Customize a web template using HTML5, JavaScript, and jQuery, then download it to your local device

I am currently working on developing a website that allows clients to set up certain settings, which can then be written to a file within my project's filesystem rather than their own. This file will then have some parts overwritten and must be saved ...

Sending Data via Ajax

I am currently working on implementing an ajax invitation script that allows users to invite their friends to an event. The javascript code I have used in other parts of the website works perfectly, but for some reason, it is not functioning correctly in t ...

Only the initial external CSS ID is functional, while the second one is inactive

I am facing an issue with my table where I have applied an external CSS that uses alternating shaded rows. My aim is to have specific table cells with different background and font colors - one cell with a green background and red font, and another with a ...

Styling pagination using CSS and jQuery

I am looking to display 3 sections in a simple, paginated way that mimics tabs. I am trying to implement the logic where when a pagination item is clicked and has the 'active' class, it should show the corresponding block. However, I am strugglin ...

Identifying the issue of body.onload not being triggered

My web pages use body.onload (or window.onload) to properly set up, but sometimes the onload event is not being triggered. Is there a way in a specific web browser (preferably Chrome, where this issue seems more common) to identify what is causing the pag ...

Unable to access frame: Error - Unable to retrieve 'add' property from undefined

I am working on customizing the chatbot functionality for my website built with Nuxt.js. I want the chatbot to display on certain pages while remaining hidden on others. Unfortunately, I encountered an issue when trying to hide it on specific pages. To im ...

Enhance your viewing experience by clicking on photos to enlarge them with

Is there a way for me to click on a photo and have it zoom in? Similar to how it works on this website --> I only need one specific photo to zoom in, not a whole gallery. Can anyone assist with this? <img src="images/uploads/phs.jpg"></img&g ...

JWPlayer has a built-in delay of 5 seconds before executing an action following the completion of a

I am looking to customize the JWPlayer so that it pauses for 5 seconds before playing the next video. Additionally, I want to display a loading animation at the center of the video similar to what is seen on YouTube. Here is the code snippet I am currently ...

Top way to include an HTML and javascript file in an Ext.Panel within Sencha Touch

My main goal is to efficiently include external HTML files and display them on an Ext.Panel in Sencha touch 2.3 by creating a wrapper module for the HTML file that can be instantiated using xtype, with an external Javascript file for event handling. Updat ...

Aligning a div within another div with an image

My current setup looks like this: HTML: <div class="wrapper"> <img src="image.jpg" /> <div class="circle"> X </div> </div> CSS: .circle { width:50px; height:50px; border-radius:50%; background: #FF0000; } ...

Ways to implement standard sorting in react-table

Currently, I am utilizing react-table v7 to generate tables. You can find more information about react-table at https://www.npmjs.com/package/react-table. While working with the table, I was able to implement sorting for all columns by following this e ...

How to ensure jQuery runs only once, even when the back button is pressed in the

How can I ensure that jQuery only executes once, even when navigating back to the page using the browser's back button? When my page initially loads, the jQuery runs fine. However, if I navigate away from the page and then return using the back button ...

AngularJS utilizes JSON objects to store and manipulate data within

My task requires accessing information from an array that is nested inside another array in Json format. Here's a more detailed example: [ { "id": 1, "name": "PowerRanger", "description": "BLUE", "connections": [ {"id": 123,"meg ...

Fetching json data from the request in Node.js

I'm currently working on a project that involves using the request module to send an HTTP GET request to a specific URL in order to receive a JSON response. However, I've run into an issue where my function is not properly returning the body of ...

FireFox - Dynamic Blinking Divs on Hover

It seems like in FireFox and IE, my hover effect is blinking for some reason. I can't figure out why this is happening. Should I switch to using JavaScript for my hovers or is there an easier CSS solution? Here's a JSFiddle link to demonstrate th ...

My preference is for the flex box to expand in width (sideways) without increasing in height (up and down)

Is there a way for flex items to expand in width but only in height when necessary? I enjoy using flexbox, but I find it frustrating that all flex items in a row grow to the same height even when there isn't enough content to fill them, resulting in a ...