Combining two kebab-case CSS classes within a React component

import React from 'react';
import styles from './stylesheet.moudle.css'

<div className={styles['first-style']} {styles['second-style']}>
some content
</div>

What is the correct way to include styles['second-style'] in the className attribute? I attempted using a comma but it did not work.

Answer №1

Problem resolved using:

<div className={`${styles['primary']} ${styles['secondary']}`}>

Answer №2

I find the classnames npm module to be very useful here

You can streamline your code and handle more intricate styling situations easily.

import classNames from 'classnames'

const flag = true;

<div className={
  classNames(
     styles.main-style,
     styles.sub-style,
     {'styles.additional-condition-class': flag})}>

Answer №3

Another option is to utilize the clsx library.

const NavigationBar = ({
  classes,
  ...otherProps
}) => {
    <Element
      className={clsx(styles.main, classes)}
      {...otherProp}
    >
    ...
}

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

Utilize fixed values in type declaration

Strange Behavior of Typescript: export type MyType = 0 | 1 | 2; The above code snippet functions correctly. However, the following code snippet encounters an issue: export const ONE = 1; export const TWO = 2; export const THREE = 3; export type MyType = O ...

Top Method for Reloading Element-Specific JavaScript When Replacing Elements

Is there a better way to re-initialize JavaScript without needing a page refresh? I'm currently struggling with appending an MDBootstrap <select> tag, which involves more than just adding a child element. Instead, I have to remove the element an ...

Guide on Implementing jQuery Plugin with Vue, Webpack, and Typescript

I am currently exploring the integration of the jQuery Plugin Chosen into my vue.js/Webpack project with TypeScript. After some research, I discovered that it is recommended to encapsulate the plugin within a custom Vue component. To kick things off, I m ...

Breaking down a jQuery array into separate JSON objects

Hi there, I am currently working on constructing a post request. Here is the format I need to send: const data = { categories: [ { id: 9 }, { id: 14 } ], } However, what I have is ["29", "23", "22&quo ...

Using JavaScript to create circular shapes on canvas with paint circle

I need assistance in JavaScript to create a circle and update it while removing the previous one. My current code is: <html> <head> <style> body { margin: 0px; padding: 0px; } </style> </head> <body> <ca ...

Verify if a username is available using jQuery

When trying to register, I use this function to check the availability of a username. If the username already exists, the submit button is disabled. The check is done using a php script that queries the database for existing usernames. Everything works per ...

Having trouble with AJAX within AJAX not functioning properly?

Similar to the Facebook commenting system, I am aiming for comments to be appended to previous ones and displayed all at once. However, currently the comments only show up after the page is reloaded. Please Note: Each post initially loads with just two co ...

Developing a fresh feature in Angular.js for transmitting my localstorage model information to a bus?

As a beginner in Angular Js, I have mastered the basics and am now working on storing user input values to localstorage using a form. Although this part is working fine, I need assistance in creating a new service to communicate with my colleague's . ...

Integrating Material UI into a React.js application

I am looking to add a CDN link for my React JS app. Can someone provide me with the link to include Material UI? http://www.material-ui.com/#/ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My First Re ...

The Star Rating System fails to update accurately after hiding the Radio Buttons

I followed a tutorial to set up a Star Rating System Everything was working fine on the SHOW PAGE and the INDEX PAGE until I decided to hide the Radio Buttons and use the corresponding labels to submit the value. I encountered an issue where the JavaScrip ...

Tips for handling two JSON array objects with JavaScript

This particular function public function groups_priviledges($user_id = NULL){ $data['groups'] = $this->priviledges_model->admin_groups(); $data['member'] = $this->priviledges_model->empAdminGroup($user_id); echo ...

Is there a way to automatically populate an AngularJS input field?

Attempting to automate filling out a website using JavaScript. The script below: document.getElementsByClassName('form-control')[1].value = "MYNAME"; The input text changes, but upon clicking the submit button it displays as empty. Any as ...

Adjust the dimensions of the Twitter Bootstrap Modal to match the maximum resolution of the user's screen

When the "Launch demo modal" button is clicked: I would like the modal to adjust its width and height based on the user's screen resolution, maximizing the available space. Additionally, I want the modal to appear in the top left corner of the docume ...

CSS: Creating a block that stretches the entire width of its parent block

I've been facing the same issue multiple times. Whenever I use the float property to display one block, the next block ends up overlapping with the first one. For example: Consider the following block of code: .row_item{ width: 30%; display: block; ...

Easily updating a SQL row with the click of an HTML button using PHP

I am in the process of creating a basic comment system that includes a rating for each comment. After reading a comment, users will be asked if they liked it with two options, "Yes" and "No". I am seeking a straightforward method to update the number of ...

Overridden CSS rules

Having a slight CSS dilemma. Within my webpage's html, I have the following structure: <div class='box-div'> <div>Entry 1</div> <div class='hide'>Entry 2</div> </div> Here is my associated ...

How to smoothly fade out content when hovering over a menu item in HTML<li> tags

Hi there, I have encountered a problem and could really use your assistance. I am attempting to create a menu that displays content when you hover over an li tag. The first content should always be visible when hovering over the first li option or if no l ...

Error in Laravel Mix Vuejs component: Syntax problem detected in <template />

My development environment involves Windows 10 OS and I have created a Laravel 8 project integrated with VueJs. However, when I try to access the file ./components/app.vue, it returns a SyntaxError: Unexpected token (1:0) The content of the app.vue file i ...

AngularJS written plugin in Javascript

I developed an app using Rails and AngularJS. A startup has reached out to me about transferring the technology to their website, but they have limited tech resources. The goal is to create a seamless integration process. The idea is to make it similar to ...

The functionality of CSS transitions does not currently apply to React MUI components

Using state variables, I have successfully set the height of a MUI Box object and it changes accordingly when the states are updated. However, I am now facing an issue where I want to create smooth transitions for the height changes. I have followed CSS r ...