I am facing an issue where my Tailwind classes are not functioning properly when passed through props, but they work seamlessly when I directly link the Tailwind CSS using

//Button.jsx

import React from 'react';

const Button = (props) => {
  let color = props.color || 'blue';

  return (
    <button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus:outline-none focus:shadow-outline`}>
      Click
    </button>
  );
};

export default Button;


// App.jsx
import Button from "./Button"
import './index.css'
function App() {


  return (
    <div>
       <Button >Click me!</Button>
      <Button color="red">Click me!</Button>
      <Button color="green">Click me!</Button>
    </div>
  )
}

export default App  

this is a component where i'm transferring background color using props

i don't know what's wrong... same code just when i add cdn link of tailwindcss in html page it work but don't work without cdn. if i will transfer bg-red-500 whole class as props then it works but if i send only red as props then it doesn't work. even in inspect page i can see classes but they're not taking effect. i'm using vite-react.

Answer №1

Don't forget to include the following steps in your main CSS file:

@tailwind base; @tailwind components; @tailwind utilities;
. Additionally, make sure to check out an extra step mentioned in the getting started guide: https://tailwindcss.com/docs/installation

Answer №2

Make sure to include the full class name for TailwindCss in your code:

const CustomButton = (props) => {
  let color = props.color || 'blue';

  // Remember to use complete and unbroken class names for TailwindCss:
  let bgColorClass = 'bg-blue-500';
  if(color === 'red'){
    bgColorClass = 'bg-red-500';
  }

  return (
    <button className={`px-4 py-2 font-bold text-black ${bgColorClass} rounded-full hover:bg-${color}-700 focus:outline-none focus:shadow-outline`}>
      Click Here
    </button>
  );
};

Learn more about dynamic class names in TailwindCss

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate a distinct identifier for every menu item within the Material UI TablePagination

Can a unique identifier be assigned to each menu item generated using the <TablePagination> component? I would like to add a specific ID (e.g., id="menu_item_0", id="menu_item_1" & id="menu_item_2") to the menu item ...

Jest is throwing an error: Unable to access property from undefined while trying to import from a custom

I developed a package called @package/test. It functions perfectly when imported into a new, empty React TypeScript application. However, issues arise within Jest test suites. The usage of the CommonJS package version causes Jest to throw an error: Test ...

Is there an equivalent of getElementById for placeholder text?

I need help automating the input of information on a webpage using JavaScript. Each field has a unique ID, like this: <div id="cc-container" class="field has-float-label"> <input placeholder="ccnumber" id="credit_card_number" maxlength="16" ...

What is the best way to incorporate user input and output functionality in a React Javascript application with Material UI for a seamless display?

I am trying to achieve a similar output as shown in this code http://jsfiddle.net/6vqd4vnq/ but using material ui/reactjs. Is there something missing in my setup that is preventing the content from being displayed correctly like in the provided link whic ...

Activate scroll event when image src changes in Angular using jQuery

Seeking assistance with utilizing jQuery to scroll to a specific thumbnail inside a modal when left/right arrows are pressed. The modal should appear when the user clicks on an image. I've managed to implement scrolling upon clicking a thumbnail, but ...

Combining two Angular expressions

I have two values to work with: {{columns.field}} and {{resultado.!!!}}. The challenge is that I need to include the value of {{columns.field}} inside the !!!, but I'm struggling to figure out how. I have to structure it this way because I am unsure o ...

What steps can be taken to fix the issue of "Module not found: Can't resolve 'firebase/storage' in next.js" even after adding firebase using yarn?

I am trying to integrate Firebase into my Next.js file, but I keep getting an error saying "module not found". Can someone help me apply Firebase in this file? import Image from 'next/image'; import { useSession } from 'next-auth/client&apos ...

How to eliminate looping animations with jQuery

While looping through items, I am encountering an issue where an animation triggers when the loop returns to the first div. I simply need a way to switch between divs without any animations on a set interval. $(document).ready(function () { function s ...

Streamlined Infinite Scrolling Data Visualization Using SVG Implemented in HTML and CSS

I have developed a dynamic graph that continuously updates with new data points being plotted. Right now, I am utilizing requestAnimationFrame() to render the element positions 30 times per second, but performance can be sluggish with numerous SVG element ...

Using loops in Sass mixins causes errors

I have been developing a SASS code to generate CSS output that has the following format. background: -webkit-linear-gradient(top, 50%); background: -moz-linear-gradient(top, 50%); background: linear-gradient(top, 50%); This is the SASS mixin code I cr ...

Using ReactJS to create different behavior for checkboxes and rows in tables with Material-UI

I am looking to customize the functionality of checkboxes and table rows. Currently, clicking on a row also clicks the checkbox, and vice versa. What I would like to achieve is for clicking on a row to trigger a dialogue box, and for clicking on the chec ...

Error: Unable to access 'length' property of null in Next.js issue

I am encountering an error in my Next.js files: 117 | }; 118 | > 119 | if (data.length <= 0) { | ^ 120 | return null; 121 | } 122 | If I want to display it with an image, the error looks like this:https://i.stack ...

Encountering a glitch in Jest unit testing post updating to npm version 3.3.12

After updating my project to node 5.1.0, npm 3.3.12, and updating all dependencies, I am now facing a plethora of errors! Originally, I encountered the same error as discussed in this GitHub issue: https://github.com/facebook/jest/issues/554, which was re ...

I'm attempting to execute a piece of code that retrieves a status count from a dataframe, the output of which will be displayed above my pandas html table

#Here is an example of a dataframe: import pandas as pd import numpy as np data = [['001', 'Completed'], ['002', 'In Process'], ['003', 'Not Started'], ['004''Completed ...

The challenge of Webpack errors arising from sharing a project between two different machines

My project is saved in a Google Drive folder and runs smoothly on one of my machines. However, when I try to compile the code on another machine, I encounter multiple errors all related to loading images. The error message below is just one example (others ...

Utilizing ajax for fetching a data table

I am new to using ajax and have successfully retrieved data from a table. However, I am now trying to pull in an entire data grid but not sure how to achieve this. In my index.php file, I have the following code: <html> <head><title>Aj ...

Steps for modifying the CSS class of an <a> tag with Jquery/Javascript

I am attempting to dynamically change the class of a tab on the dashboard based on the selected page. In the dashboard, there are 3 tabs: <div> <ul class="menu"> <li class="MenuDashboard"><a href="#" >Dashboard</a&g ...

What is the best method for transforming a stream into a file with AngularJS?

Currently, I have a server returning a file stream (StreamingOutput). My goal is to convert this file stream into an actual file using AngularJS, javascript, JQuery, or any other relevant libraries. I am looking to display the file in a <div> or ano ...

Looking to retrieve the values of a 3-level dropdown using ajax. Successfully fetched the value of the 1st dropdown, but unsure of how to retrieve the values of the 2nd and

I have successfully managed to retrieve all values, but when the 2nd and 3rd drop-downs are populated through Ajax, how can I access those values? I am looking for a way to fetch the values of the 2nd and 3rd drop-down without resorting to session variable ...

Ensure images are correctly aligned

Could really use some help with this issue I'm having. It's probably a simple fix for all you experts out there, but I can't seem to align my images properly so they are even and not one higher than the other. Please take a look at the scree ...