My approach to using this style in React involves utilizing the class attribute as follows: "class[attribute]" [Updated version 2]

When trying to customize an element based on its attribute, the process is a bit different in React compared to pure CSS. Here's an example:

<div class='something' data-test='4'></div>
...
.something[data-test] {
  background-color: red;
}

In React, you would need to adjust your code like this:

<div dataTest={0} className={classes.something} >
    Lorem Ipsum
</div>
...
    something: {
      "&[dataTest]": {
        backgroundColor: 'red',
      }
    }

If you encounter issues with the conversion, double-check your syntax and ensure that it matches the expected structure.

PS: Other attempted variations include:

"& [data-test]": {
"&.[data-test]": {

Answer №1

It is important to remember to utilize className instead of class. Afterwards, in your CSS:

.something[data-test="4"]  {
  background-color: red;
}

Answer №2

If you're looking to access data attributes in react, here's a helpful guide along with a demo.

return (
    < >
      <div className="test" data-id="4">
        <h1>Hello everyone</h1>
      </div>
    </>
  );

styles.css

.test {
  border: 1px solid red;
}

div::before {
  content: attr(data-id);
}

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

Upon triggering a GET request to the URL "http://localhost:3000/search", a "404 (Not Found)" error response was received. Interestingly

Can Someone assist me please? I'm having trouble creating a website similar to YouTube and encountering the following error: GET http://localhost:3000/search 404 (Not Found) Uncaught (in promise) Error: Request failed with status code 404 at createEr ...

What is the best way to send a socket.io message to a particular user who is currently

When using socket.io with express and typescript, I am facing an issue where emitting to a particular logged-in user is not working. However, the rest of the functionality works fine such as when a new user joins. In my App.ts backend file, the code looks ...

Define the total in a CSS attribute

I am working with multiple divs that have a specific CSS class applied to them. My goal is to manually increase the pixel value in the top property: .class { top: 100px; } div class="class" style="top:+=50px" Is it possible to achieve this functiona ...

Displaying input fields in editable cells of a react-table should only happen upon clicking the cell

I've been experimenting with the react-table library and my goal is to create editable cells that only show an input box for editing when clicked. Unfortunately, I'm running into an issue where the input box appears constantly instead of just on ...

Guide to comparing the contents of two text fields and highlighting the altered characters using ReactJS

Is there a way to compare the contents of two Material-UI textfields and identify the characters that have changed in both? I came across a similar question, but it was specifically for ReactJS rather than C# Windows Forms: How can you highlight the chara ...

Box size adjusts to fit its content, including any form fields

Looking to create a box that adjusts its size based on content and center it without resorting to using tables for layout or setting fixed sizes. How can this be achieved while keeping the markup clean? I've almost got it, but the form fields are not ...

Include previous input as a "phantom" thumbnail to the slider element of type "range"

https://i.stack.imgur.com/JnuCN.png Using a tutorial from w3schools, I have customized a regular range slider. The objective is to send a new value command to an external MQTT-based home automation system and display the previous value as a ghost-thumb in ...

Adjusting the width of a div element horizontally in Angular 4 and

As someone new to Angular 4, I've been attempting to create a resizable div (horizontally) without success. My goal is to be able to click and drag a grabber to resize the text area horizontally. However, in the example provided in the link below, the ...

The render() method in a class does not support Jquery functionality, unlike the componentDidMount() method where it works effectively

After updating my packages to the latest versions, I encountered an error in my project. Previously, everything was working fine, but after upgrading Next.js to version 9 and jQuery to version 3.5, the $.map function stopped working as expected. When I tri ...

Step-by-Step Guide on Redirecting NextJS Page to SPA

As I work on developing a SaaS application with Next JS, my goal is to leverage SSG for SEO purposes on landing pages and pricing. However, when it comes to the actual application, I prefer using a SPA due to its high interactivity. Initially, I am relucta ...

Transforming three items into an array with multiple dimensions

There are 3 unique objects that hold data regarding SVG icons from FontAwesome. Each object follows the same structure, but the key difference lies in the value of the prefix property. The first object utilizes fab as its prefix, the second uses far, and t ...

I need to sort my tags by comma and use MUI Select to filter them

[ {id: 'AjRzfMxbfJMphK144DIAr', title: 'javascript', tags: 'code,programining', notes: "This book is a must to", createdAt: 1671459053853} {id: 'kvdo7HrLr2GeOUX9j4qLq', title: 'css', tags: &ap ...

Unpacking objects within an array in the backend of a Next.js application

How can I resolve this issue? I am passing the req.query parameter with an array but I am unable to destructure or use items from the array. In my Next.js API backend, I only get [object Object] or undefined. How can I access and select specific values fro ...

React's componentDidMount fails to trigger for jQuery AJAX request

Here is my code snippet: import React from 'react'; import {render} from 'react-dom'; class App extends React.Component { constructor(props) { super(props); this.state = { data: '' }; ...

Get the selected row value from a Table and convert it into an array. Then, pass this array as a parameter to another function

I need help with logging and storing selected values from a Table component in React My Table component displays data from an API with checkboxes on each row. I'm using the <Table /> Material UI component for React. The documentation states th ...

Dynamic sidebar that adheres to the content and is placed alongside it using JavaScript

I am in need of creating a sticky sidebar that will float beside my content column. The purpose of this sidebar is to contain jump links on the page, similar to what can be seen on this page, but with the navigation buttons positioned directly next to the ...

What are the steps to clipping a canvas using CSS clip-path?

When it comes to clipping a canvas, there are multiple methods that can be used. One way is to create a path with getContext('2d') and set globalCompositeOperation. Another method involves using -webkit-clip-path or clip-path, though the latter m ...

Issue encountered while trying to modify the color of a placeholder

Whenever I attempt to modify the placeholder's color: ::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } : ...

Trouble aligning 'nav' to the center of the page in the metro-bootstrap.css file

Struggling with alignment issues in my navigation list while using 'metro-bootstrap.css' from . Despite numerous attempts, I can't seem to get it right. Visit advinadv.co.uk for a closer look. Any assistance would be greatly appreciated! Be ...

Ways to incorporate vertical alignment while adjusting the size of a <div> block

Seeking advice on how to vertically align <div> blocks (green blocks in my example) when changing block size. What adjustments are needed in my example for the vertical alignment (middle) of side blocks to be maintained when resizing the browser win ...