🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to React Notes
Topic #19

React JSX Attributes


JSX allows you to insert attributes into HTML elements, but there are a few important differences.


class = className

The class attribute is a much used attribute in HTML, but since JSX is rendered as JavaScript, and the class keyword is a reserved word in JavaScript, you are not allowed to use it in JSX.

JSX solved this by using className instead. When JSX is rendered, it translates className attributes into class attributes.

Example

function Car() {
  return (
    <h1 className="myclass">Hello World</h1>
  );
}

Expressions as Attributes

You can also use JavaScript expressions as attribute values. This is very useful for dynamic attributes.

Example

function Car() {
  const x = "myclass";
  return (
    <h1 className={x}>Hello World</h1>
  );
}

Note that the attribute value is not wrapped in quotes, this is important when using expressions (JavaScript variables) as attribute values. If you use quotes, JSX will treat it as a string literals and not a JavaScript expression.


camelCase Event Attributes

Event attributes in JSX are written in camelCase.

Example

function Car() {
  const myfunc = () => {
    alert('Hello World');
  };
  return (
    <button onClick={myfunc}>Click me</button>
  );
}

Boolean Attributes

If you pass no value for an attribute, JSX treats it as true. To pass false, you must specify it as an expression.

Example

<button onClick={myfunc} disabled>Click me</button>

Example

<button onClick={myfunc} disabled={true}>Click me</button>

Example

<button onClick={myfunc} disabled={false}>Click me</button>

The style Attribute

The style attribute in JSX only accepts a JavaScript object with camelCased CSS property names, rather than a CSS string (as in HTML).

Example

function Car() {
  const mystyles = {
    color: "red",
    fontSize: "20px",
    backgroundColor: "lightyellow",
  };

  return (
    <>
      <h1 style={mystyles}>My car</h1>
    </>
  );
}

Notice two things about the example above.

  1. The styles are stored in an object.
  2. Style properties are written in camelCase, e.g. fontSize, instead of font-size.

This is an important difference between HTML and JSX.

You will learn more about CSS and styling in the React CSS Styling chapter.

Want to go beyond the notes?

Join CodingNow 2.0's React course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

React JSX Attributes – FAQs

Quick answers about learning React JSX Attributes in React.

This free note from CodingNow 2.0 explains React JSX Attributes in React — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every React topic on CodingNow 2.0, including React JSX Attributes, is 100% free with no signup required.
With focused practice, most students grasp React JSX Attributes in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now