Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
React adding additional className via props does not follow correct order?

I have a custom component in one file, lets say like that:

import styles from './CustomComponent.module.css'

export default CustomComponent = ({className, onClick, text, ...props}) => {
    return (
        <div className={styles.outer}>
            <div className={`${styles.inner} ${className}`}>{text}</div>
        </div>
    )
}

That component is imported into another file, which has it's own components and css module:

import styles from './ParentComponent.module.css'
import CustomComponent from '../CustomComponent/CustomComponent.component.js'

export default ParentComponent = () => {
    return (
        <>
            This is an example.
            <CustomComponent text='Example' className={styles.overrideStyle} />
        </>
    )
}

What I want to happen is that the overrideStyle can be used to add css parameters and/or override some existing ones of the container style. What happens is that new parameters are added, if were not previously defined in the inner style, but already existing ones are not overwritten.

For example, if the CustomComponent css is something like:

.outer {
    width: 100%;
    padding-top: 20px;
}

.inner {
    height: 100px;
    width: 100%;
}

and overrideStyle is something like this:

.overrideStyle {
    height: 500px;
    padding: 20px;
}

I expected the resulting css properties of the CustomComponent inner div to behave as follows:

.(combined) {
    height: 500px;
    width: 100%;
    padding: 20px;
}

What I get is:

.(combined){
    height: 100px;
    width: 100%;
    padding: 20px;
}

New properties are added, but existing ones are not overwritten.

What rules are there that I don't understand and how can I force the className that is passed through props to supersede the inner one?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There is a typo in your CustomComponent it should be

<div className={${styles.containerStyle} ${className}}>{text}</div>

instead of only <div className={${styles.container} ${className}}>{text}</div>

Change styles.container to styles.containerStyle in CustomComponent

about 4 years ago · Juan Pablo Isaza Report

0

One fix provided by a friend of mine is to mark the properties of overrideStyle with !important:

.overrideStyle {
    height: 500px !important;
    padding: 20px !important;
}

This does not solve the problem, but it provides a quick and dirty fix for now.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!