Activate CSS modules in ReactJS by incorporating a .module extension to the CSS file

Being new to ReactJS, I recently learned about enabling CSS modules and discovered the following:

If you append .module to a class name, it will generate a base64 hash string for that class

As an example, I created a function-based component called Layout.js

import React from "react";
import classes from "./Layout.module.css";

const layout = props => <div className={classes.bg}>This is the first div</div>

export default layout;

and its corresponding css file Layout.module.css

.bg {
  display: inline;
  background-color: rgba(115, 251, 4, 0.685);
}

When inspecting the div in the browser:

<div class="Layout_bg__1bzIA">This is the first div</div>

Everything appeared to work properly. However, when I created another component named second.js and used the same class on a div within it

import React from "react";
import classes from "../Layout/Layout.module.css";

const second = props => <div className={classes.bg}>This is the second div</div>

export default second;

Upon inspecting the second div, it showed:

<div class="Layout_bg__1bzIA">This is the second div</div>

Both elements ended up with the same hash value. This raises the question of whether my initial understanding was correct

The use of .module enables CSS modules

If that's the case, why are different elements in different components receiving the same hash values?

Answer №1

In my interpretation, modules provide the ability to generate identical class names without causing any interference between them. This means that when you create a new module with the same class name (like in your case with "bg"), it will be assigned a unique hash value. As a result, these two classes will always remain separate and distinct from each other. I hope this explanation clarifies things for you.

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

Differences in rendering with Google Chrome when zooming in or out

After creating some code, I observed a peculiar issue. At 500% zoom, the DOWNLOAD button aligns perfectly with the left wall without any gap. However, upon reducing the zoom to 250%, a green background segment becomes visible. To witness this anomaly, refe ...

Inflexible lists refusing to wrap

I have a straightforward requirement: I need a list <ul /> containing items <div />s of the same fixed size. These items should be displayed from left to right, filling up all available space before wrapping onto the next line. Below is the ba ...

Deactivate the connection button for accessing the database

I'm currently developing a PHP script to establish a connection with a MySQL database. My goal is to deactivate a button and display a spinner image when I click on a submit button to initiate the database connection. Below is the code snippet I have ...

How to Set a Background Image for a Div in React?

render(){ const { classes } = this.props; const { imageDescription } = this.props.imgDesc; const { currentIndex } = this.state; var fullPath = `../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`; con ...

Unable to launch React Native project

Error: Module Not Found Cannot find module 'C:\Users\Admin\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\@babel\parser\lib\index.js'. Please make sure that your package.jso ...

I ran into an issue trying to generate a React app using the command "npx create-react-app" and was unable to proceed

When I attempted to run the command npx create-react-app my-app, I encountered an error: (ps: I also tried running npm init and npm install create-react-app before executing the aforementioned command, but still got the same error.) HELP! Installing pack ...

Looking for a drum set with clickable buttons? Having trouble changing the background color in CSS/HTML? Wondering how to map keyboard keys to HTML buttons?

Behold my HTML creation: <H1> <center> EPIC GUITAR JAM </center> </H1> <img class="guitar" src="guitar.jpg" /> <button class="strum" onclick="Strum()"> Strum Chord </button> <button class="pluck" o ...

Tips for adjusting the MUI datagrid's display mode specifically for mobile users

In my React project (version 18.2.0), I'm utilizing the MUI datagrid from the community edition version 5.17.22. My goal is to adjust how the datagrid renders rows and columns specifically for mobile devices. To better illustrate my point, I have incl ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

What is the best way to display the most recent data from a sqlite database and incorporate animations for a more engaging

Hi there! I'm looking to retrieve data from an SQLite database and display only the latest entry. Additionally, I'd like to create an animation where scrolling reveals one entry at a time. I would greatly appreciate any assistance with this. Pl ...

Collapse or expand nested rows within a dynamic table using Bootstrap Collapse feature

I am currently working on creating a dynamic leaderboard table for a sports league using data from a SQL database. The league consists of multiple teams, each team has different players (with some players belonging to more than one team), and players earn ...

Discover the mistake during the creation of the next.js application build

Trying to build my next.js file, but encountering an error: ./pages/city/[city].js 21:20 Error: React Hook "useRouter" is called in function "city" that is neither a React function component nor a custom React Hook function. React ...

The nested CSS grid is failing to show its nested elements on the page

Having trouble with a nested CSS GRID - the child grid items are not appearing correctly despite multiple checks on the markup. The first grid row should span three columns, as it does, and is set to display:grid with 2 child grid items. However, the chil ...

Learn how to set up webpack or Next.js to generate a CSS file from custom Material styles and automatically inject it into the header

One of the things I've done is define some custom material styles, like this: import {createStyles, makeStyles, Theme} from '@material-ui/core/styles'; export const useStyles = makeStyles((theme: Theme) => createStyles({ fab ...

utilize console.log within the <ErrorMessage> element

Typically, this is the way the <ErrorMessage> tag from Formik is utilized: <ErrorMessage name="email" render={(msg) => ( <Text style={styles.errorText}> ...

Issue with arranging cards in a grid when utilizing v-for alongside bootstrap

Just starting out with vuejs and attempting to create a grid by looping through objects using bootstrap classes. However, I'm running into an issue where the cards are not forming a grid as expected when utilizing the col classes. Below is the code f ...

What is the process for verifying user authentication in a GET request?

My frontend utilizes Reactjs, while the backend is built with Nodejs and expressjs, connected to a Postgresql database. I have a basic signin page that authenticates users from the database. In my Reactjs application, once signed in, users can upload files ...

Implement real-time updates on the React frontend application by utilizing the state hook in React.js to dynamically

While I was making changes to my code in order to ensure that the update request works smoothly without refreshing the react frontend, things didn't go according to plan. Here's where I currently stand: On the "Profile" component page, I created ...

How can I create my own custom pagination and sorting features instead of relying on the default ui-bootstrap options?

Can anyone provide assistance with resolving conflicts I am experiencing while using ui-bootstrap? ...

The positioning of the text appears to be in the center, but it is actually

Currently, my text "lorum ipsum" is centered in CSS, but I want to align it to the left and add padding on the right later. Can someone explain what I've created here? #ubba { font-family: "Open Sans", sans-serif; font-size: 20px; ...