0

I have a question regarding how to correctly use .scss modules(and common css modules) when it comes to use the same .scss module in several components at once.

For instance: if a parent & its children need to access the same exact .scss module, which way is the best to access the module?

Assume I have a .scss module which contains all styles and a component AudioPlayer that has a structure like this:

 import audioPlayerModule from './SCSS/AudioPlayer.module.scss';
 /*Some code*/
 return (
    <div className={audioPlayerModule.audio_container}>
        <LeftControls/>
        <CenterControls />
        <RightControls/>
    </div>)

The main AudioPlayer component uses the module audioPlayerModule. Then let's say I need this module again inside the child component LeftControls:

import audioPlayerModule from './SCSS/AudioPlayer.module.scss';
const LeftControls = () => {
      return (
      <div className={audioPlayerModule.left_controls_container}></div>
);
}

Thus I have imported the same .scss module "audioPlayerModule" to parent & each of its children. Is there any better way to do it without using "props.children"?

0

1 Answer 1

1

You can pass className props to your child components

<LeftControl className={audioPlayerModule.left_controls_container}/>

and then spread this props in child

const LeftControls = ({props}) => {
      return (
      <div {...props}></div>
);
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't passing down the className to each child produce a lot of trash code due to prop drilling?
With the approach I mentioned, prop drilling is happening only at one level. If only that's your use case I think it pretty much acceptable to prop drill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.