代码之家  ›  专栏  ›  技术社区  ›  MrXQ

在React中动态添加类名。有顺风。css

  •  0
  • MrXQ  · 技术社区  · 4 年前

    假设我有一个React组件,它从 props 例如:

    import React from "react";
    
    export default function Header({navColor}) {
    
      return (
        <nav
        className="flex justify-center items-center text-white  text-xl h-14"> //I want to add a class that it's name is the (navColor) value to the nav tag 
          TEST
        </nav>
      );
    }
    

    如何才能做到这一点?

    1 回复  |  直到 4 年前
        1
  •  1
  •   Shan    4 年前

    你可以用 Template literals 为了实现这一点

    使用 ${} 在…内 背虱 ``

    <nav
        className={`flex justify-center items-center text-white  text-xl h-14 ${navColor}`}> 
          TEST
    </nav>