Combining Node and React: Using CSS Class Names with Hyphens

Currently, my project utilizes react-starter-kit and includes several react components, each with its own styling file (scss). Every component imports the styling file at the top like this:

import s from './Header.scss';

When it comes to using css classes without hyphens (e.g. 'notification'), there are no issues. However, I am struggling to use hyphenated css classes:

render() {
 return (
  <div className={s.header-inner}> </div>
 );
}

Unfortunately, this results in an error: 'inner is undefined'.

I attempted to change header-inner to header_inner both in my html and in the component, which worked fine. However, due to the large number of classes in my css file, manually changing them all is not feasible.

If anyone has any suggestions or solutions, your help would be greatly appreciated.

Thank you!

Answer №1

- is not a recognized identifier character, which means your original code would be interpreted as:

s.header - inner

Since you have not declared a variable named inner, you will encounter a reference error.

Nevertheless, any character can be utilized to form a key for an object, so you can use a string to access the desired property:

return (
  <div className={s['header-inner']}> </div>
);

Answer №2

You have the ability to assign a CSS class by using a dot without needing to enclose it in brackets.

Check out https://github.com/webpack/css-loader

If you configure webpack's css-loader like this:

css-loader?camelCase

Then you can utilize it in code like so:

<div className={s.headerInner}> </div>

Answer №3

s['header-inner']

When working with JavaScript, dot notation can be convenient but it may not work for all variable names. In cases where the variable name is illegal, it's best to use bracket notation instead.

Another tip to keep in mind is that CSS styles in JavaScript should be written in camelCase format. For example, if you're setting a style like this:

<div style={{ backgroundColor: '#FFF', zIndex: 3 }} />

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

Matching multiple divs with different ids in PHP using preg_match_all and regex

I have an HTML page structured like this: <!DOCTYPE html> <html> .... <body> <div class="list-news fl pt10 "> Blue </div> <div class="list-news fl pt1 ...

Chrome won't display the downloaded file until at least 8 bytes have been sent, unlike Firefox which shows it immediately

My goal is to implement an HTTP method that sends a file to the user, but there is a delay of 4 seconds required to generate the file content. The challenge I'm facing is that Chrome only displays the file as being downloaded after 8 bytes are sent. U ...

React - discrepancy in date-fns' differenceInDays function resulting in a NaN output

For some reason, I am encountering a NaN error when attempting to calculate the difference in days between the current date and a product's expiry date in the user's timezone. I have included my code below for reference: import { formatInTimeZone ...

"Using jQuery to add emphasis to a table row and remove it again

I have a table with multiple rows, where I have styled each odd row in one shade of gray and the even rows in slightly different shades to make it easier to read. When clicking on a row, I am currently highlighting it with a different color to indicate wh ...

Struggling to locate text within the confines of the meta viewport

So I'm new to this whole blogging thing and I wanted to make sure my site was responsive. I added the meta viewport tag, but when I tested it out on Google Chrome with different window sizes, my text just disappeared! Here's a snippet of the CSS ...

Could you assist me in retrieving information from an API request?

I can't seem to pinpoint my mistake, but I know it's there. After the user provides their state and city information and submits it, a fetch request is supposed to retrieve latitude and longitude values. These values are then used in another API ...

What is the best way to redirect users to the login page when they are logged out from a different tab or window?

Ensuring user authentication and managing inactivity are crucial components of my Nodejs application, where I leverage cookie-session and passport.js. app.use(require("cookie-session")({ secret:keys.session.secret, resave:false, saveUninitiali ...

The authorization process for uploading data to Azure Data Lake Gen2

Currently, I am working on generating a Shared Access Signature (SAS) client-side within my Node.js application. The primary goal is to allow users to directly upload files to my Azure Data Lake Gen2 Blob Storage container without streaming them through th ...

The WebsocketServer.js in my React project is throwing a SyntaxError when it comes to

I have set up a React project using webpack-dev-server, and here is the content of my package.json file: { "name": "testproject", "version": "1.0.0", "description": "", "main": & ...

Set the first link in a menu to be highlighted as active using jquery

In order to mark the first link in my menu as active, I need it to have specific CSS settings applied. Using jQuery to add these settings to every link when clicked on makes it a bit tricky (simple menu = clicking changes color). So, I want the first link ...

Generating JSON on-the-fly with fluctuating keys and values in conjunction with Express JS

When I fetch an API into my Express server, the data comes in the form of JSON key-value pairs within an array. [{ "quality": "best", "url": "https://someurlhere.example/?someparameters" }, { "quality": ...

Module not found: The system encountered an error and was unable to locate the file 'os' in the specified directory

I'm currently working on a Laravel/Vue3 project and ran into an issue. When I try to execute "npm run dev", I encounter 37 error messages. I suspect it has something to do with the mix or webpack configuration, but being unfamiliar with this area, I&a ...

"Enjoy a unique browsing experience with a two-panel layout featuring a fixed right panel that appears after scrolling

I am facing difficulty in designing a layout with two panels where the left panel has relative positioning and the right panel becomes fixed only after a specific scroll point. Additionally, I need the height of the right panel to adjust when the page scro ...

"Unlocking the power of Material UI: optimizing element alignment for seamless side-by-side display

Currently, I am working on an ecommerce project where I am using Material UI for styling. I am facing a challenge in getting all elements like Typography and Buttons to display side by side in a row rather than vertically. I'm wondering if there is a ...

Could there be an issue with my website's bootstrap where badges are not being applied properly?

Currently, I am in the process of learning ReactJS and running into an issue where my bootstrap is not working within my .jsx file. Despite following a tutorial (MOSH) diligently and extensively researching on stack overflow, I have not been able to find a ...

Tips for Choosing Certain List Items in a Responsive Bootstrap Navigation Menu for WordPress

I've put together a sleek bootstrap navbar menu for WordPress. If you want to check out the menu design, you can view it by clicking on this link: here There are still some tweaks that need to be made though. Specifically, I need to add a box around ...

Message: "IE11, React-Router, and Adobe Analytics encountered an Invariant Violation stating: "You shouldn't utilize <Switch> outside of a <Router>"

While trying to load a React App that is functioning correctly in all other browsers, an error message appears in IE 11 when Adobe Analytics is present on the same page: Invariant Violation: You should not use <Switch> outside a <Router> This ...

Adaptable layout - featuring 2 cards side by side for smaller screens

I am currently designing a webpage that showcases a row of cards, each featuring an image and a title. At the moment, I am utilizing the Bootstrap grid system to display 4 cards per row on larger screens (col-md-2), but my goal is to modify this layout t ...

Ways to expose a declared module in Enzyme's shallow rendering?

I've been grappling with how to ensure that my custom declared module is detected when I shallow render a component using enzyme in Jest unit tests. The issue revolves around a specific module declaration named _aphrodite, which looks like this: // i ...

What will happen if I have multiple nested SWRConfig components with different options selected?

I am currently utilizing SWRConfig to implement a global fetcher, but I also have the requirement to override this fetcher in certain components. In such a scenario, would the options specified at a higher level of SWRConfig be applied? <SWRConfig ...