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

200
Views
How I can create a simple React component rendered as a <div> which takes a text value and a foreground color as input

I'm new in the world of React and after see tutorials and made some codes I wanted to know if I can add attributes like value and color to a div. I mean, How I can return Hello World with color. The follow is an example:

<div style="color: red;">Hello World!</div>

I tried with constructor function, but I can't render the color and the value of "Hello World!

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

0

In a JSX file you'd use <div style={{color: 'red'}}>Hello World!</div>

about 4 years ago · Juan Pablo Isaza Report

0

I would use a class for this.

function Example() {
  return (
    <div className="red">
      Hello world!
    </div>
  );
}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
.red { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

Or, if you're passing props into another component, you can still use this method. Just pass the colour as a prop, and then use className={color}.

function Example() {
  return <Hello color="red" />
}

function Hello({ color }) {
  return (
    <div className={color}>
      Hello world!
    </div>
  );
}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
.red { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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!