Styling React components with localized CSS files

After diving into a React project built with redux-easy-boilerplate (https://github.com/anorudes/redux-easy-boilerplate/tree/master/src), I found the structure of individual SCSS files for each component quite appealing. As I migrate an older app that uses LESS for styling, I aim to repurpose its styles within this new project.

My attempt to simply swap out SCSS with LESS in the boilerplate has hit a snag that leaves me puzzled. Looking at a standard component like this one: https://github.com/anorudes/redux-easy-boilerplate/tree/master/src/components/Footer, we see it contains a styles directory housing two files -

index.js:</p>

<pre><code>import 'style!./styles.scss';
export default require('./styles.scss').locals.styles;

and a styles.scss file structured as follows:

:local(.styles) {
  text-align: center;
  padding: 70px 0 40px;
  ...
}

Renaming the .scss file to .less triggers an error indicating an inability to access the 'styles' property (referencing locals) on the second line of index.js. Lack of familiarity with SCSS makes it challenging to comprehend the purpose of the initial line in the SCSS file and identify an equivalent in LESS syntax.

Furthermore, I confirmed the addition of the LESS loader to the webpack configuration:

{
  test: /\.less$/,
  loader: 'style!css!less',
},

Answer №1

I have identified a couple of issues in this particular situation.

Firstly, there was a replacement made for the loader used with SCSS files:

loader: 'css?localIdentName=[path]!postcss-loader!sass'

The new loader implemented for Less files is as follows:

loader: 'style!css!less'

However, these two loaders are not interchangeable. The initial configuration utilized the sass loader to convert SCSS syntax to standard CSS, followed by post-processing with postcss-loader, and then converted to plain CSS using css?localIdentName while also creating local scopes based on the query parameter ?localIdentName=[path]. This information can be found in the css-loader documentation:

CSS exports all class names into a global selector scope by default. However, you can utilize a local selector scope using the syntax :local(.className). Local identifiers are exported by the module.

Additionally, you can configure the generated ident by specifying the localIdentName query parameter (default is [hash:base64]). For example:

css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]
for easier debugging purposes.

If the query parameter is omitted, the .locals.styles property will not be present in the required CSS.

Furthermore, it is advised that the style-loader should not be included in your loader configuration to prevent automatic style injection. Instead, styles should be required so that access to locals is available for additional injection. This explains why the boilerplate recommends the following setup within styles/index.js for each set of styles:

import 'style!./styles.scss';
export default require('./styles.scss').locals.styles;

The first line injects the styles, while the second line exports the locals.

To resolve this issue, I recommend updating your webpack configuration to:

test: /\.less$/,
loader: 'css?localIdentName=[path]!postcss-loader!less',

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

"Bootstrap 4 does not allow labels to be displayed inline with the input field

I'm currently working with Bootstrap 4 and Vue.js to populate an input form, but I'm struggling to get the label to appear inline and before the input type file. <div v-for="problem in problems"> <div class="row"& ...

How to Target HTML Tags Locally using CSS Modules in Next.js?

I am looking to implement smooth scrolling specifically on one page in my Next.js application, such as my blog. Instead of applying it to the entire site through the globals.css file, I need a way to inject scroll-behavior: smooth; into the html tag only f ...

Ways to eliminate the gap preceding <br> code

My footer contains two sections: one for the address and one for contact information. The address section is styled using <p> tags with <br/> elements inside, while the contact information section is styled using <span> elements with a di ...

When using threejs, the color set for setClearColor is supposed to be white. However, when calling an external HTML file, it unexpectedly renders as

When I call an external HTML file in Three.js, the value for setClearColor is white but it renders as black. How can I solve this issue? Click here to view the image Here are the codes from the external file: <div id="3d-modal"></div> <sc ...

Restructuring Firebase JSON data within a React Native environment

UPDATE: I had to modify this question after gaining more information about my initial problem. We currently have data in the following format: { "-fdsdsghdfsgsfdfdgfd" : { "latitude" : -37.830331, "longitude" : 144.9923426, "found" : false ...

The module specifier for React could not be resolved when attempting a dynamic import from the API

I'm currently experimenting with dynamically importing a React component library from an API. I've successfully fetched the JS file and transpiled it using Babel without any issues. When I try to import the Dummy.js file from localhost like impor ...

Displaying blob files in a React application

I am facing an issue with displaying a blob file using React. I have retrieved the file from a MySQL database and attempted to display it, but unfortunately, I keep encountering an error stating that the document could not be loaded (refer to the image). ...

Change the css code to override the color of the background material

Hey there, I'm facing an issue with overriding the background color of a material element. I've tried several methods but can't seem to get it to change. After inspecting the element on the web browser, I found the CSS code that controls th ...

How do you retrieve specific colored elements from an array using jQuery (or plain JavaScript)?

I am currently in the process of creating interactive checklists. One feature I have implemented is changing the color of a header if all checkboxes associated with it are checked, as shown below: $("input[type='checkbox']").click(function(){ i ...

Div refuses to clear floats

Working on an app at and encountering an issue with the main container not clearing floats, causing overflow on the content. Attempted to use this CSS: .clearfix:after { content:""; display: table; clear: both; } Added the class to the main ...

Display an indicator in CSS to show when a line has wrapped

Can CSS be used to visually indicate when a line is wrapped? For instance, consider the following code inside a "pre" block: <html><body>Hello World</body></html> I have set white-space to pre-wrap to prevent horizontal scrolling ...

A step-by-step guide on toggling between light mode and dark mode using water.css

Implementing the water.css on my website, I have this JavaScript code: function setTheme(themeName) { localStorage.setItem('theme', themeName); document.documentElement.className = themeName; } // function to toggle between light and dark ...

How can you make a Unity Web GL html container adjust its width to match the size of the browser window?

Looking to create a Unity WebGL build that allows for specifying width and height in the HTML template. How can I dynamically set the width of div id="gameContainer" based on the browser window width to ensure it always fills the space? .webgl-content ...

What could be the reason for data.map not being recognized as a function?

I developed the following React component: import React, { useState } from 'react'; export default function Example(){ var friend = function (id, firstName, lastName){ this.id = id; this.firstName = firstName; this.lastName = lastN ...

Map display issue with Leaflet

Having trouble getting the leaflet map to take up the full screen. I'm aiming for a header bar and the map filling the rest of the space. I've experimented with different options such as setting the height to 100%. Any suggestions or ideas? Thank ...

Having trouble getting padding to work inside a pre block, even when using a wrapper div?

Snippet of HTML Code: <div class="prewrap"> <pre> stepsize = .01 samplestimes = 30 universex = seq(-1, 1, stepsize) universey = sin(pi * universex) </pre> </div> Sample CSS Styles: #prewrap { background-color: #e3e3e3; pa ...

Unable to change Mui-selected background Color

I'm trying to change the background color of the selected pagination item in Material UI, but it's not working as expected. Sometimes it displays the correct color momentarily, but reverts back to the incorrect color after refreshing the page. ...

The custom CSS classes are being taken over by Antd styles through the use of useServerInsertedHTML in next.navigation

Initially, I encountered a problem where the antd styles on my page were taking 1 to 2 seconds to load, causing my page to render incorrectly during that time. However, thanks to the helpful guidance provided in this answer about the solution to the slow A ...

Demonstrate the proper implementation of a Stepper component using Material UI in a React.js

I am trying to display a responsive screen using "progressive forms" with React.js and Material Ui. I have implemented the Stepper component for this purpose, but when I click the "Next Button", the button is hidden but the next step content with the text ...

Errors are thrown when utilizing hydration with RTK Query

Looking for a solution with my local API and RTK Query, I've encountered an issue when implementing server-side rendering (SSR). Here's the code snippet I'm working with: const api = createApi({ reducerPath: 'data', baseQuery: ...