简体   繁体   English

在 next.js 中将 props 作为 classNames 传递

[英]passing props as classNames in next.js

I am trying to have the header of each of my app's pages change color based on the current page.我试图让我的每个应用程序页面的 header 根据当前页面更改颜色。 How I am trying to achieve this:我如何努力实现这一目标:

<Header className="headerBitcoin"></Header>

What I want is the be able to have the header component present on all 4 pages, and then just change the className to another prop to change the background but not anything else.我想要的是能够让 header 组件出现在所有 4 个页面上,然后只需将 className 更改为另一个道具即可更改背景,但不会更改其他任何内容。
And the header component itself以及 header 组件本身

import styles from "../../styles/Home.module.css";
export default function Header(props) {
  return (
    <div >
      <div className={props.className}>aaaaa</div>
      <div className={styles.row}>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
        <div className={styles.tab}>a</div>
      </div>{" "}
    </div>
  );
}

At the moment the styles for the tabs and row are working but the header is not getting its style applied.目前,选项卡和行的 styles 正在工作,但 header 没有应用其样式。
I checked the console and found the header is getting the className headerBitcoin passed to it, however the row beneath it has the className of "Home_row__88lPM"我检查了控制台,发现 header 正在将 className headerBitcoin传递给它,但是它下面的行的 className 为"Home_row__88lPM"
This is my first time working with next.js, and I know I am doing something wrong because this works in React.这是我第一次使用 next.js,我知道我做错了什么,因为这适用于 React。 Any help would be appreciated.任何帮助,将不胜感激。

don't do this:不要这样做:

<div className={props.className}>aaaaa</div>

try this instead:试试这个:

<div className={styles[props.className]}>aaaaa</div>

I think this should works我认为这应该有效

I assume it's not being applied because you have the headerBitcoin styles defined in your CSS module.我认为它没有被应用,因为您在 CSS 模块中定义了headerBitcoin styles。

You'll need to define them in your global CSS for it to be applied that way.您需要在全局 CSS 中定义它们才能以这种方式应用。

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

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