0

I have created a separate file of some data and importing in different files. With the map function, I am printing this data in the

  • tag. But want to use this data on different pages with different CSS like in some pages I need color changes or different font size. No event requires. Just want to print data of same component but with different CSS on every page.

        `const Truck = [
        {
            icon: <i className="fa fa-truck fa-flip-horizontal"></i>,
            text: "Door pick-up and delivery",
        },
        {
            icon: <i className="fa fa-truck fa-flip-horizontal"></i>,
            text: "Heavy weight shipments",
        },
        {
            icon: <i className="fa fa-truck fa-flip-horizontal"></i>,
            text: "Same day delivery in city limits",
        },
    ]
    export default Truck`................
    
    Map function
    
    }`export const UlServicesItems = () => {
        return (
            <div id="UlMarginTop">
                {Truck.map((item, index) => {
                    return (
                        <div id="txt">
                            <li key={index} className="iconTruck">{item.icon}</li>
                            <li key={index} className='newId'>{item.text}</li>
                        </div>
    
                    )
                })}
            </div>`enter code here`
        )
    
  • 1
    • Use props to the component to tell it what kind of style it should have Commented May 21, 2021 at 12:10

    1 Answer 1

    2

    You could define some sort of theme and pass it as props to your component.

    First create some CSS classes.

    .bg-green {
      background-color: green;
    }
    
    .bg-red {
      background-color: red;
    }
    
    .big-text {
      font-size: 20px;
    }
    

    In your parent component, define themes and pass it as props to the components.

    import "./styles.css";
    import Card from "./Card";
    
    export default function App() {
      const theme = {
        backgroundclass: "bg-green",
        textclass: "big-text"
      };
    
      const theme2 = {
        backgroundclass: "bg-red",
        textclass: "big-text"
      };
    
      return (
        <div className="App">
          <Card theme={theme} />
          <Card theme={theme2} />
        </div>
      );
    }
    

    In your child component, use the theme prop and set the values to the corresponding elements.

    export default function Card({theme}) {
      return (
        <div className={theme.backgroundclass}>
          <p className={theme.textclass}>hello</p>
        </div>
      );
    }
    

    Sandbox link

    Sign up to request clarification or add additional context in comments.

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.