The React Component in Next.js does not come equipped with CSS Module functionality

I have been attempting to incorporate and apply CSS styles from a module to a React component, but the styles are not being applied, and the CSS is not being included at all.

Here is the current code snippet:

./components/Toolbar.tsx

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

export default function Toolbar({title = "Social Media App"}) {
    return (
        <div className={styles.toolbarContainer}>
            <h1>{title}</h1>
        </div>
    )
}

./components/Toolbar.module.css

.toolbar-container {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 20px;
    background: grey;
}

./app/layout.tsx

import type {Metadata} from 'next'
import {Inter} from 'next/font/google'
import React from "react";
import Toolbar from "@/components/Toolbar";
import Homepage from "@/app/page";
const inter = Inter({subsets: ['latin']})

export const metadata: Metadata = {
    title: 'Some Social Media App',
    description: 'Some new kind of social media outlet'
}

export default function RootLayout({children}: { children: React.ReactNode }) {
    return (
        <html lang="en">
        <body className={inter.className}>
        <Toolbar title="Homepage" />
        {children}</body>
        </html>
    )
}

./app/page.tsx

import React from "react";
import Link from "next/link";

export default function Homepage() {
    return (
        <>
            <p>Welcome to the homepage!</p>
            <Link href={"/dashboard"}>Dashboard</Link>
        </>
    )
}

What could be causing the CSS from the Toolbar.module.css file to not be rendered on the page?

I have tried moving the Toolbar around to different locations and files, as well as including it directly in the layout.tsx file, but nothing seems to work.

Answer №1

Firstly: The reason for this issue is that hyphens are allowed in CSS class names but not in JavaScript when used for subtraction.

Secondly: It is important for the selector in the CSS file to match the reference in the React component!

This example works perfectly:

In React component:

<div className={styles.toolbarContainer}>

In CSS file: .toolbarContainer{ ... }


This scenario does not work (both are valid but the values do not match):

In React component:

<div className={styles.toolbarContainer}>
VALID

In CSS file: .toolbar-container{ ... } VALID


This situation also does not work due to the explanation provided in the first point above:

In React component:

<div className={styles.toolbar-container}>
NOT VALID

In CSS file: .toolbar-container{ ... } VALID

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

How can you prevent the blur event from triggering in jQuery when the next element to receive focus is of a specific type?

I am currently developing a unique form UI where I am working on creating a custom select box replacement. The custom select control consists of a hidden select input within a div, along with anchor elements inside a span that mimic the options of the sele ...

Saving an embedded document within another document in MongoDB using Mongoose

Our project requires us to save a duplicate of a Mongo document as a nested subdocument in another document. This copy should have a direct link to the original document and needs to be a complete replica, like taking a snapshot of the original data. The ...

Is there a way to obtain the current URL within the index.html file using Vue.js?

How can I retrieve the current URL in my index.html file using Vue.js? When using pure JavaScript in index.html, I am only able to obtain the URL of the initial page. In order to capture the URL of other pages, I need to refresh the page as the value of ...

Does the react-google-login library utilize the services provided by Google Identity?

Currently incorporating the react-google-login library (https://www.npmjs.com/package/react-google-login/v/5.2.2) in my JavaScript codebase to grant users access to my website. Can anyone confirm whether this library utilizes "Google Identity Services" or ...

Hyperlink -> Boundary

Our homepage features various elements as part of the menu. Each element consists of a picture, title, and small description. The issue is that these menu items always display a border when the mouse hovers over them, despite attempts to hide it. You can ...

JavaScript shortening a string while joining it

I'm facing a challenge with string truncation in my laravel and JS(jquery) app. Initially, I suspected it was an issue with the backend (as indicated in my question here: Laravel Truncating Strings). However, after thorough debugging, I discovered tha ...

Chrome Devtool reported an error, but the majority of them are in a null file

Currently grappling with an irksome problem in Vue.js. No matter how hard I try, I just can't seem to pinpoint the error. Even when setting a debugger, all it shows is an empty file. Please take a look at the image provided. Any assistance on identify ...

Combining Various Time Periods in JavaScript

After encountering various old solutions, most utilizing jQuery, for adding multiple durations together, I decided to create my own script below. While I may not be a JS expert, I am open to input on any potential issues with this code or a more efficient ...

Using Node modules in the browser: A beginner's guide

Once you've used npm to install packages such as: $ npm install bootstrap or $ npm install angular these packages are typically stored in the "node_modules" directory. The question now arises, how do you link to or access them from an HTML page? U ...

To determine if an AJAX request is synchronous or asynchronous using Browser Developer Tools

Is there a method to verify if a specific ajax request is asynchronous or synchronous using Browser Dev Tools such as Chrome Developer Tools or Firebug? The HTTP Request Header for an ajax request does not specify whether it is sync or async. X-Request ...

Show a loading progress indicator with a percentage until Next/React completes loading

For my upcoming SPA project, I will be fetching a 3D map and data from an API. Is there a method to incorporate a loading banner that displays the current percentage of files, components, or data loaded before the application is completely loaded? I am in ...

The import component path in Angular 4/TypeScript is not being recognized, even though it appears to be valid and functional

I'm currently working on building a router component using Angular and TypeScript. Check out my project structure below: https://i.stack.imgur.com/h2Y9k.png Let's delve into the landingPageComponent. From the image, you can see that the path ...

Why is jQuery not defined in Browserify?

Dealing with this manually is becoming quite a hassle! I imported bootstrap dropdown.js and at the end of the function, there's }($); Within my shim, I specified jquery as a dependency 'bootstrap': { "exports": 'bootstrap', ...

Exploring the concept of front-end deletion through ajax technology

I'm currently tackling a front-end CRUD RESTful API project and encountering challenges specifically related to the DELETE and PUT methods. While I have successfully managed to implement the GET and POST functionalities, utilizing a forms.html file f ...

Is there a way to access the current $sce from a controller?

One way to access the current $scope outside of a controller is by using the following code: var $scope = angular.element('[ng-controller=ProductCtrl]').scope(); Is there a way to retrieve the $sce of the current controller? ...

Speed up the process of distributing files to clients using Node.js Express

In my opinion, Node.js Express is the most efficient and speediest method to create a rapid web application for prototyping. With just a few lines of code and some HTML/JS files, you can have a functional web app up and running in no time. However, one iss ...

The scrollable dialogue disrupts the background's ability to flex and grow

My page layout is pretty simple: <main> <header>Header</header> <section>Content</section> <footer>Footer</footer> </main> <dialog>Dialog</dialog> In terms of CSS, here's how it ...

Alter the row's color using the selection feature in the module

Is there a way to change the color of a row when a specific property has a certain value? I found a solution for this issue on Stack Overflow, which can be viewed here: How to color row on specific value in angular-ui-grid? Instead of changing the backgro ...

What is the best way to retrieve a file's creation date using the file System module in Node.js

I'm having trouble fetching the filename and file creation date to send to a client. I tried using fs.stat which provides birthtime but does not include the filename. So, my question is: is birthtime equivalent to the file created date? How can I sen ...

How to utilize View as a substitute for the div tag in your Web Project

Undoubtedly, when working on a web project, it is common practice to use the div element like this: <div> sample text </div> However, using div in React Native can lead to errors: <View> sample text </View> Is there a way to ...