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

271
Views
How to inject JavaScript inside JSX attribute's string?

I'm trying to put some default props inside className=" px-6 py-2 bg-black rounded-full text-black " and when I use this component elsewhere I will be passing background or text color as props.

What's the right syntax for this manipulation?

I was trying to use grave accent ` with classic ${} but it doesn't work out in my react application.

<button className=`btn px-6 py-2 bg-${} rounded-full`>
  shop
</button>

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You'd use a JSX expression, which is {expression} (in a JSX context). So you might well use a template literal, within the {}, like this (assuming backgroundColor and textColor contain the props):

return (
    <Button className={`px-6 py-2 bg-${backgroundColor} text-${textColor}`}>
        ...
    </Button>
);

It doesn't have to be a template literal, it could be a string concatenation expression:

return (
    <Button className={"px-6 py-2 bg-" + backgroundColor + " text-" + textColor}>
        ...
    </Button>
);

Or to keep the JSX from getting to hard to work with, do it prior to the JSX:

const theClass = `px-6 py-2 bg-${backgroundColor} text-${textColor}`;
return (
    <Button className={theClass}>
        ...
    </Button>
);
about 4 years ago · Juan Pablo Isaza 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!