Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Is it possible to set a className on custom react components?

Let's say I have a simple component like this:

class SimpleComponent extends React.Component
{
    render() { return <p>Some text</p> }
}

Is it possible to add a className to SimpleComponent or is this constrained to HTML DOM elements only?

For example:

var mySimpleComponent = <SimpleComponent className="myComp"/>

The reason I would like to do this is so that I can style the elements inside my custom component like this:

.myComp > p {
    background-color: blue;
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can, but you should propagate the prop to the inner component. If not, it doesn't know about it.

class SimpleComponent extends React.Component
{
    render() { return <p className={this.props.className}>Some text</p> }
}

To make the CSS query you want to accomplish, then you should create the div inside your component.

class SimpleComponent extends React.Component
{
    render() { return <div className={this.props.className}><p>Some text</p></div> }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!