Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass style as props in react component

I created a event-box component in react. What I want is whenever is it called, I passed color values as props which later get used to set its border. Currently, I set two hardcoded class names and passed them as props with the component. Is there any other way to do so as I won't able to add all the color class names in my stylesheet.

Component code


import React from 'react';

class EventBox extends React.Component{
    constructor(props)
    {
        super(props);
        this.state={

        }
    }
    render()
    {
        const style={
            marginBottom:'0px'
        }
        const list={
            display:'inline-flex',
            width:'100%',
            marginBottom:'10px'
        }
        const listItem={
            flex:'1',
            display:'flex'
        }
        return(
            <div className={this.props.class}>
                <ul className="list list-inline" style={list}>
                    <li className="list-inline-item color-golden" style={listItem}>1 March 2020</li>
                    <li className="list-inline-item color-red flex flex-end" style={listItem}>200 People Interested</li>
                </ul>
                <h3 className="sub-heading roboto">Title</h3>
                <p className="paragraph roboto" style={style}>Saket, New Delhi</p>
                <p className="paragraph roboto" style={style}>Time: 05:00 P.M - 06:30 P.M</p>

            </div>
        )
    }
}
export default EventBox;


 <EventBox class="col-md-12 event-box-container red-border" />
 <EventBox class="col-md-12 event-box-container green-border" />

CSS

.event-box-container.red-border{
    border-top: 8px solid red;
}
.event-box-container.green-border{
    border-top: 8px solid green;
}

like image 309
Pranay kumar Avatar asked Apr 15 '20 10:04

Pranay kumar


People also ask

How do you pass styles as prop React?

Use the React. CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React. CSSProperties; . The CSSProperties type is used to type the style object that consists of CSS property names and values.

Can you pass a React component as prop?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!

Can I pass props to styled component?

To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.


1 Answers

You can just pass the dynamic styles to the component as an object.

For Class-Based Components:

import React from 'react';

class EventBox extends React.Component{
    render() {
        return(
            <div className="col-md-12 event-box-container" style={this.props.style}>
            </div>
        )
    }
}
export default EventBox;

For Functional Components:

import React from 'react';

const EventBox = ({ style }) => {
    return(
        <div className="col-md-12 event-box-container" style={style}>
        </div>
    )
}
export default EventBox;

Usage

<EventBox style={{ borderTop: '8px solid red' }} />
<EventBox style={{ borderTop: '8px solid green' }}  />
like image 117
Anatol Avatar answered Oct 09 '22 07:10

Anatol