Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda