Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to style child Component when child and parent both use css.module? #408

Open
wine-fall opened this issue Mar 11, 2024 · 1 comment
Open

Comments

@wine-fall
Copy link

wine-fall commented Mar 11, 2024

parent.tsx

import React from 'react';
import {Child} from './Child';
import styles from './index.module.less';

const Parent: React.FC = () => {
    return (
            <Child className={styles.child}></Child>
    );
};

export default Parent;

child.tsx

import React from 'react';
import styles from './child.module.less';
interface ChildProps {
    className?: string;
}

export const Child: React.FC<ChildProps> = (props) => {
    return (
        <div className={props.className}>
            <span className={styles.childP}>123</span>
        </div>
    );
};

child.module.less

.childP {
    color: black;
}

and in index.module.less which imported in parent.tsx file

.child {
    :global {
        .childP{
            color: red;
        }
    }
}

It doesn't work??? why ??? and what should I do?

@fisschl
Copy link

fisschl commented Mar 13, 2024

The classes in the child.module.less file will be hashed to avoid conflicts.
So childP in both files is not the same class.
Maybe you can move these classes into the same file and remove :global.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants