Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

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

Use attribute className instead of class in JSX:

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

Run Example »


Expressions as Attributes

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

Example

Use JavaScript expressions as attribute values:

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

Run Example »

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

Use camelCase for event attributes:

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

Run Example »


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

Boolean true in JSX, this will make the button disabled:

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

Run Example »

Example

Also true in JSX, this will also make the button disabled:

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

Run Example »

Example

False in JSX, this will NOT make the button disabled:

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

Run Example »


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

Use the style attribute:

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

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

Run Example »

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.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.