JSX Attributes
A JSX attribute is a named value on a JSX tag. It works like an HTML attribute and it sets a detail of the tag.
An image tag
The src and alt attributes describe the image.
return <img src='cat.jpg' alt='A cat' />A class name
className sets the class of the heading.
return <h1 className='title'>Hello!</h1>Example
export default function App() {
return <h1 className='greeting'>Hello, React!</h1>
}Key points
- An attribute is a named value that sits inside the opening tag.
- The src attribute of an img tag holds the image file name.
- The alt attribute of an img tag holds text that describes the image.
- className is the JSX name for the HTML class attribute.
In this example, App returns a heading. The heading has a className attribute. className sets the class to greeting. React turns className into class in the HTML.
Note
JSX uses className because class already has a job in JavaScript.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which JSX attribute sets the class of an element?