如何将CSS模块与React使用并将样式保持在相同的(.jsx)文件中?

发布于 2025-02-09 14:27:51 字数 585 浏览 2 评论 0原文

我想在.jsx文件中编写我的 CSS模块。类似于样式组件。这是我要实现的示例(请注意,这是一个文件):

// Cmp.jsx file

const styles = ???`
  .wrapper {
    display: flex;
      
    .name {
      color: blue;
    }
  }
`

export default function Cmp() {
  return (
    <div className={styles.wrapper}>
      <div className={styles.name}>Bob</div>
    </div>
  )
}

是否有可以做到的库?

我可以从CSS模块的WebPack加载程序导入一些内部功能以代替????如果不是这样,请提示如果我想实现它应该查看的位置(实际上执行CSS模块导入的代码)将很有帮助。

请注意,样式的组件可以(AB)用于以类似的方式编写,但这无助于避免classNames/样式的碰撞。

I'd like to write my css modules in .jsx file. Similarly to styled-components. Here is an example of what I want to achieve (note that this is a single file):

// Cmp.jsx file

const styles = ???`
  .wrapper {
    display: flex;
      
    .name {
      color: blue;
    }
  }
`

export default function Cmp() {
  return (
    <div className={styles.wrapper}>
      <div className={styles.name}>Bob</div>
    </div>
  )
}

Is there a library that allows to do it?

Can I import some internal function from webpack loader of css modules to use in place of ???? If not then a hint on where I should look if I want to implement it (the code that actually does the import of css modules) would be helpful.

Note that styled components can be (ab)used to write in a similar manner but it won't help to avoid collisions of classnames/styles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文