Recognized by Clutch.co as a top-rated Mobile App Development Company.
folio3-mobile
US 408 365 4638
START YOUR PROJECT
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us

Using LESS in React without Ejecting

Published by: Noc Folio3 | May 7, 2019
SCROLL AND BE AMAZED!
Home > Featured Posts > Using LESS in React without Ejecting
Using LESS in React without Ejecting - Enterprise Blog folio3

I recently came across a problem while configuring my React application to use LESS styling. The misleading information on the web made the solution a bit tricky. So, after finding the appropriate solution, I decided to write this article.

Some background

CSS has always been an essential part of the Web. It brings order and color to the dull world of HTML. However, writing CSS can get very repetitive and little tasks such as having to look up hex color values, closing your tags, etc. can become time-consuming. To counter these problems, the CSS community developed CSS Preprocessors. With a CSS preprocessor you can:

  • Write cleaner CSS with more reusability
  • Structure your CSS so that it is easier to understand
  • Save time
  • Easy code maintenance with snippets and libraries
  • Write simple logics/calculations within your CSS

As an example take a look at the following LESS snippets

// variables
@width: 10px;
@height: @width + 10px;

// code structure
#header {
  color: black;
  .navigation {
    font-size: 12px;
  }
  .logo {
    width: @width;
    height: @height;
  }
}

// Mix-ins (reusability)
.bordered {
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}
#menu a {
  color: #111;
  .bordered();
}
.post a {
  color: red;
  .bordered();
}

You can find more examples HERE.

There are a number of CSS preprocessors in the market but most used are these

  • Sass
  • Less
  • Stylus
Using LESS in React without Ejecting - Enterprise Blog folio3

Since LESS and SASS are not actually CSS, a browser cannot understand them. They need to be compiled to CSS before being served. In context of React, this is fine for production as compiling your LESS one time before production is not a problem. But in the development environment, where you make changes frequently, this can really get tedious and boring. Therefore, our problem comes down to avoiding/automating the compilation process each time we change our LESS/SASS.

The solution

Mostly React developers use ‘create-react-app’, a utility from Facebook, for setting up their react environment. With [email protected] and higher, React provides out of the box support for SASS.

To use SASS with your React application, all you have to do is add SASS dependencies

"dependencies": {
   "node-sass": "^4.11.0"
 }

Then you can import your SASS stylesheet like this

@import 'stylesheet.scss'

However, there is no such solution for LESS, at the moment. So In this article we will focus on creating a solution for LESS. Actually, it’s better to call this a work-around instead, as it has some negative aspects, like having to import your LESS file (say “style.less”) like “style.css”.

To solve this problem, we will set up a LESS-CSS compiler to compile a LESS each time we make changes to the stylesheet and put the compiled CSS file into the exact same directory so that the user can use the compiled CSS file instead by just changing the extension from LESS to CSS while importing the stylesheet. Configuring webpack to do this is a headache as we have to “eject” before being able to make any configuration changes to the webpack. Ejection is an irreversible process that’s why developers avoid it. So we will not use this solution.

As a workaround, we can use a utility called less-watch-compiler. It can watch for LESS files in a source directory and compile them to CSS placing the output files into an output directory.

Add following dev-dependencies

"devDependencies": {
   "less": "^3.9.0",
   "less-watch-compiler": "^1.13.0"
 }

Less-watch-compiler takes a config file as parameter

{
   "watchFolder": "src/",  
   "outputFolder": "src/",  
   "sourceMap": true,
   "runOnce": false,
   "enableJs": true
}

Here we are telling less-watch-compiler to watch all files in ‘src’ and its sub-directories for changes and if any file is changed, output the compiled CSS file into the same directory.

If we start this less-watch-compiler once we have to start our React server, every time we change any LESS file in our project, we will have an updated CSS file in the same directory. So, it is better if we start it in our “start” script. But since start script will also be used in production we create another script named ‘dev-start’. To run the react server concurrently with the less-watch-compiler we use a utility called ‘concurrently’.

Add concurrently to your dev-dependencies

"devDependencies": {
   "concurrently": "^4.1.0"
}

Out dev-start script will be

"dev-start": "concurrently --kill-others \"less-watch-compiler --config less-watcher.config.json\" \"react-scripts start\""

The “–kill-others” flag means if any one of the two processes dies the other will be killed too.

THAT’S ALL. Change your “App.css” to “App.less” and start using LESS styling. Please note that we will import “App.css” instead of “App.less”. Your CSS file will always be up to-date, thanks to the less-watch-compiler.

A complete code for a sample project is available athttps://github.com/sferhan/less-react-sample

Please feel free to reach out if you have any questions. In case you need any help with development, installation, integration, up-gradation and customization of your Business Solutions. We have expertise in Enterprise Application Development Solutions. Connect with us for more information. [email protected]


About Noc Folio3

Newsletter

Search

Archives

  • December 2023
  • April 2023
  • March 2023
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • October 2021
  • September 2021
  • May 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019

Recent Posts

  • Exploring Flutter Navigation: From Basics to Advanced Routes
  • Web UI Test Automation with Pytest-BDD
  • How to fix IOS compass calibration issues
  • Testing Android Applications With Perfect Coverage
  • How to use useRef hook efficiently? – React

Tags

  • android
  • angular-state-management
  • Automation
  • Compass
  • cross-platform
  • css
  • development
  • firebase
  • hooks
  • ios
  • learn-ngrx
  • ngrx-beginner
  • ngrx/store
  • QA
  • react-native
  • reactjs
  • scss
  • stylesheet
  • styling
  • Testing
  • Test Script
  • UI-UX

Newsletter

Newsletter

Post navigation

Next Cool new ES6 features that every JavaScript Developer must know.
Schedule an Appointment with our Mobile App Development Expert
Footer Menu
  • Company
    • About Us
    • Portfolio
    • Blog
    • Careers
    • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise App Development
    • AR/VR Application Development
    • IoT Application Development
    • Wearables Apps Development
    • Field Sales
    • On-Demand Apps Development
  • Technologies
    • iOS
    • Android
    • React Native
    • Flutter
    • Ionic
    • Xamarin
    • NativeScript
    • HTML5
    • Sencha
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical
    • Manufacturing
    • Automotive
    • Logistics
    • Education

US Office

Belmont, California – 1301 Shoreway Road, Suite 160, Belmont, CA 94002

Pleasanton, California – 6701 Koll Center Parkway, #250 Pleasanton, CA 94566

Tel: +1 408 365 4638
Support: +1 (408) 512 1812

Mexico Office

Amado Nervo #2200, Edificio Esfera 1 piso 4, Col. Jardines del Sol, CP. 45050, Zapopan, Jalisco, Mexico

Bulgaria Office

49 Bacho Kiro Street, Sofia, 1000, Bulgaria

Canada Office​

895 Don Mills Road, Two Morneau Shepell Centre, Suite 900, Toronto, Ontario, M3C 1W3, Canada

UK Office

Export House, Cawsey Way, Woking Surrey, GU21 6QX

Tel: +44 (0) 14 8361 6611

UAE Office

Dubai, UAE – Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

Tel: +971-55-6540154
Tel: +971-04-2505173

Pakistan Office

Folio3 Tower, Plot 26, Block B, SMCH Society, Main Shahrah-e-Faisal, Karachi.

First Floor, Blue Mall 8-R, MM Alam Road Gulberg III, Lahore.

Tel: +92-21-3432 3721-4 

© 2025, Folio3 Software Inc., All rights reserved.

  • Privacy policy and terms of use
  • Cookie Policy
Follow us on
Facebook-f Linkedin-in Instagram

Get a free app audit

[contact-form-7 id="3548" title="Float Banner Form"]