Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a css inside an html tag in styled components? (react)

I have a react application where I receive through an API, a category. Within that category, I have "color" where a css color is sent.

In "category" I would like to pass the css color that is sent within the information I receive in the api, so for that I must pass the color within p id="cat" with the "color"

import * as C from './styles';

import { useNavigate } from 'react-router-dom';

const Card = ({img, date, title, category, color, id}) => {  
    const navigate = useNavigate();

    const handleSinglePost = () => {
        navigate(`/singlepost/${id}`)
    }

  return (
    <C.Card onClick={handleSinglePost}>
        <C.ImgContainer>
            <img src={img} alt="" />
            <C.Cat>
                <p> {date}</p>
                <p id='cat'> {category}</p>
            </C.Cat>
            <h2>{title}</h2>
        </C.ImgContainer>
    </C.Card>
    )
}

export default Card;

That is, in theory pass color as background-color style of p

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

something like:

<C.Cat>
  <p> {date}</p>
  <p id='cat'style={background-color: color}> {category}</p>
</C.Cat>

It certainly doesn’t work that way, but it’s an example of what I’d like it to be and how I’ve tried to illustrate

>Solution :

Try to write:

<p id='cat'style={{backgroundColor: color}}> {category}</p>

style property accepts an object with camel-cased css properties.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading