Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color not rendering from props

So, I'm passing in props to my JSX component, and then setting that props into a gradient from black to that prop. But whenever I try this, the gradient ends up going from black to just a transparent background.

import React from 'react'

import Color from './color'

const App = () => {
    return (
        <div className="h-screen w-screen">
            <Color color="red-400" />
        </div>
    )
}

export default App

import React from 'react'

const color = props => {


    return (
        <div className="h-screen w-screen">
            <div className={`h-full w-full absolute bg-gradient-to-r from-cyan-500 to-${props.color}`}>
                {props.text}
            </div>
        </div>
    )
}

export default color

What should I do?

like image 433
LonelyPepper Avatar asked Feb 18 '26 20:02

LonelyPepper


1 Answers

Tailwind statically scans your files. It cannot interpolate strings. So whenever you pass a class, you have the pass the whole thing. 'to-red-500' instead of `to-${'red-500'}`

Following changes should make it work(should probably update the prop name from color to say tocolor):

import React from 'react'

import Color from './color'

const App = () => {
    return (
        <div className="h-screen w-screen">
            <Color color="to-red-400" />
        </div>
    )
}

export default App
import React from 'react'

const color = props => {


    return (
        <div className="h-screen w-screen">
            <div className={`h-full w-full absolute bg-gradient-to-r from-cyan-500 ${props.color}`}>
                {props.text}
            </div>
        </div>
    )
}

export default color
like image 133
Shreshth Avatar answered Feb 20 '26 08:02

Shreshth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!