Is there a way to incorporate multiple dynamic classes in CSS modules react?

I have integrated CSS Modules

import style from "./styles/question.module.css";

// status[0] can range from 0 to 4
<p className={style[`difficulty_${status[0]}`]}>{difficulty}</p>,

The code above is functional, however the following code is malfunctioning.

// attempted to add more classes
<p className={`${style.difficulty} ${style[`difficulty_${status[0]}`]}}`}>{difficulty}</p>

Only the difficulty class is being applied.

Answer №1

If you're looking to handle classes in a more efficient way, consider using multiple options:

  • One option is to utilize the classnames library, which allows for easy handling of class conditions. Simply import the library and start utilizing it in your code. For example: https://i.sstatic.net/TZVaS.png

  • Another approach is to use join to compile all the necessary classes together.

  • You can also build all required classes outside of the className attribute and then pass them in, making it easier to identify any mistakes and allowing for better control over the values being used.

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

No validation errors coming from the Laravel controller when using React and Inertia

I am attempting to receive validation errors in a React component when a form is empty, but I am not getting any validation errors. This function is called when the form is submitted: `const handleSubmit = (e) => { e.preventDefault() const formData = n ...

Is there a way to retrieve a modified next-auth session within the app router in next.js version 13-14

After implementing the Next.js 14 app router structure, I have been working on modifying my session within the [...nextAuth].ts middleware using basic JWT and Session callbacks. While the session retrieval with the useSession callback on the client-side w ...

What steps are involved in building a modal pop-up in Angular without using any pre-existing libraries

I am looking to create a modal popup when a button is clicked. I want to achieve this without relying on any additional dependencies. The method I have used below successfully creates a fully functional modal, but I am unsure if this is the best way to do ...

beginning with --keepAliveTimeout the following

I was following the documentation at https://nextjs.org/docs/api-reference/cli and I have my script defined like this: "scripts": { "dev": "next dev", "build": "next build", "start": & ...

Using Formik with React-phone-input-2 causes the values to return as an empty string

Is there a way to integrate Formik with React-Phone-Input-2 in the phone field without encountering issues related to HandleChange, HandleBlur, and values.phone being submitted as an empty string? How can this be effectively managed? import { useFormik } ...

No JavaScript needed for this CSS framework

I have been tasked with creating a website without the use of JavaScript, following very specific instructions. Furthermore, it must be compatible with iPhone and other mobile devices, while still adhering to the no JavaScript rule. I am open to utilizing ...

Ways to update a value within an array of objects using Redux

I am facing an issue with my Redux State where I have stored an array of objects. Each object contains a key named price, and when I increment the quantity button, I need to access the specific object in Redux and update the price value. Although I was abl ...

Tips for toggling the visibility of a list of items in a React component

Struggling to toggle visibility of specific elements in a React project. I'm working with a dynamic list of li's, each containing a label. Clicking on a li should hide the label and display an input field. Usually achievable with jQuery as shown ...

Managing the state in NextJS applications

I've scoured the depths of the internet in search of a solution for this issue, but unfortunately I have yet to come across one that effectively resolves it. I've experimented with various state management tools including: useContext Redux Zusta ...

The type 'number[]' is lacking the properties 0, 1, 2, and 3 found in the type '[number, number, number, number]'

type spacing = [number, number, number, number] interface ISpacingProps { defaultValue?: spacing className?: string disabled?: boolean min?: number max?: number onChange?: (value: number | string) => void } interface IFieldState { value: ...

Trigger a distinct modal or popper when button is clicked

In my project using the material-ui library, I am dealing with a situation where I have poppers inside a loop and each loop contains an event object stored in cards. The issue arises when I try to open a popper based on a button click placed on each card; ...

Error components are missing, please refresh the page. This message is specific to Next.J

After each pull, I always follow the same steps: git init git pull npm i or npm install Initially, all pages function correctly as expected. However, when transitioning from one page to another on localhost, I encounter this error: missing required error ...

How to automatically open JQuery Accordion panels when the page loads

My Accordion loads with the 1st panel open upon page load, but I am looking for a way to have the entire Accordion closed by default. Any help or suggestions on how to achieve this would be highly appreciated. Thank you for your assistance. FIDDLE http:// ...

Error message: Unable to access properties of an undefined value (looking for 'source') in react version 16

I'm encountering an issue when trying to retrieve data from the commerceJS API in a different module and passing it down as a prop. The problem arises specifically when I attempt to access a nested product object, resulting in the error message: Uncau ...

Issues with Amplify forwarding fonts in a Next.js application are causing problems

Currently, I am referring to the Amplify Docs from AWS in order to configure redirects for my Next.js application Despite successfully implementing the redirects for a React app, when I try to do the same for the Next.js app, it results in a 404 error on ...

The distinction in the CSS Top Property behavior between Internet Explorer and Firefox

I've been searching for the answer to this question everywhere, but I haven't found a solution yet. The browsers I have been using for testing are: Firefox 3.6.8 Internet Explorer 6.029 I am trying to create a box and position it using the CSS ...

Incomplete Website Encryption Alert

Currently, I am developing a website called "usborroe.com" and have just set up a GeoTrust Business ID SSL Certificate. However, despite my efforts to ensure full encryption on the site, an error message is indicating that it is not fully encrypted and t ...

Send the user to the appropriate subdomain within a ReactJs application

I'm currently working on a React website project where I want to assign a unique subdomain to each user after they sign up based on the username they input. For example, if their username is john, then their subdomain would be john.mydomain.com. I&ap ...

Tips for implementing a toggle feature for an ion-card element

I am looking to create a toggle effect on ion-card, where clicking on the card will highlight it until either unclicked or another card is clicked. Similar to this - https://i.sstatic.net/MH5WA.png How can I achieve this effect on ion-card? Here is my H ...

Best practices for updating the value of a specific key within an object that contains recursion in JavaScript/TypeScript

I have a tree component that uses the following data structure type TreeNode = { id: string, parentId: string, renderer: () => React.ReactNode, expanded: boolean, children?: Array<TreeNode>, } Now, I am looking to add functionality for ...