简体   繁体   English

如何正确使用带有 React 组件的 .scss 模块

[英]How to properly use .scss modules with React components

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.当涉及一次在多个组件中使用相同的 .scss 模块时,我有一个关于如何正确使用 .scss 模块(和常见的 css 模块)的问题。

For instance: if a parent & its children need to access the same exact .scss module, which way is the best to access the module?例如:如果父级及其子级需要访问相同的 .scss 模块,那么访问该模块的最佳方式是什么?

Assume I have a .scss module which contains all styles and a component AudioPlayer that has a structure like this:假设我有一个 .scss 模块,其中包含所有样式和一个具有如下结构的组件 AudioPlayer:

 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.主要的 AudioPlayer 组件使用模块 audioPlayerModule。 Then let's say I need this module again inside the child component LeftControls:然后假设我在子组件 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.因此,我已将相同的 .scss 模块“audioPlayerModule”导入父级及其每个子级。 Is there any better way to do it without using "props.children"?不使用“props.children”有没有更好的方法?

You can pass className props to your child components您可以将 className 道具传递给您的子组件

<LeftControl className={audioPlayerModule.left_controls_container}/>

and then spread this props in child然后把这个道具传播给孩子

const LeftControls = ({props}) => {
      return (
      <div {...props}></div>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM