bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

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> );
}

Expressions as Attributes

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

Use JavaScript expressions as attribute values

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.

Use camelCase for event attributes

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

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

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

Example

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

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

Example

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

<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).

Use the style attribute

function Car() {
 const mystyles = {
 color: "red", fontSize: "20px", backgroundColor: "lightyellow", };
 return ( <> <h1 style={mystyles}>My car</h1> </> );
 }

Notice two things about the example above.

  • The styles are stored in an object.
  • 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 .

Previous

React JSX Expressions

Next

React JSX If Statements

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

class = classNameExpressions as AttributescamelCase Event AttributesBoolean AttributesThe style Attribute