Adding a className dynamically in React.js with Tailwind.css

MrXQ

Let's say I have a React component that gets the Tailwind class name from props for example :

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>
  );
}

How can achieve this?

Shan

You can use Template literals to achieve that

Use ${} inside backticks ``

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Tailwind in React/Next.js doesn't generate css for text-*xl classes if variable is used in className

React App adding Pure CSS to Tailwind CSS

Text is not centering in React JS (and Tailwind CSS)

React and Tailwind CSS: dynamically generated classes are not being applied

How to dynamically change border color in next js with tailwind css?

Less wont apply Tailwind CSS classname with dot (.)

What is the least performance impacting approach to wrapping lines in react className attr when using tailwind css?

Tailwind css - use className custom spacing value as variable for tailwind config

CSS transition not working when I set a className dynamically in React

Navbar in React and Tailwind CSS

Tailwind CSS not adding translate styling

How can I dynamically update className with React js?

Adding and removing className dynamically in an inifinite loop

Why can't I pass variable as a className to tailwind-css?

Unable to render Tailwind CSS properties dynamically

Passing className to a React/Tailwind component is not overriding the default classes

Adding style attributes to a css class dynamically in react app

Dynamically adding and removing CSS

adding css dynamically in jquery

How to change the primary color of the app from app's UI in React JS(Next JS) using tailwind CSS

Dynamically adding components in react

React tailwind, cannot pass tailwind css from parent to child

Dynamically adding CSS keyframe from JS to add randomness to animation

Tailwind CSS: Referencing to custom color in tailwind.config.js

How to apply CSS module with a dynamically created className

Adding className but css is not affecting in ReactJS Components

Setting className string dynamically Javascript \ React

How to dynamically change className of a component in React?

Adding event handlers to specific classname in iframe in react